2004-01-10 19:07:06 +00:00
|
|
|
//===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
|
2005-04-21 21:48:46 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:48:46 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
2004-07-25 18:07:36 +00:00
|
|
|
// to a vector of unsigned char, then copies the vector 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. :( :( :(
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WriterInternals.h"
|
2002-07-23 19:56:44 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2005-05-06 22:34:01 +00:00
|
|
|
#include "llvm/CallingConv.h"
|
2004-01-14 23:36:54 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2006-01-25 23:08:15 +00:00
|
|
|
#include "llvm/InlineAsm.h"
|
2004-07-25 18:07:36 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
2004-07-25 18:07:36 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2004-11-06 23:17:23 +00:00
|
|
|
#include "llvm/Support/Compressor.h"
|
2005-08-17 19:34:49 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2006-11-29 00:19:40 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2006-06-07 23:18:34 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-01-10 19:10:01 +00:00
|
|
|
#include <cstring>
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <algorithm>
|
2004-01-10 18:49:43 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-08-17 07:45:14 +00:00
|
|
|
/// This value needs to be incremented every time the bytecode format changes
|
|
|
|
/// so that the reader can distinguish which format of the bytecode file has
|
|
|
|
/// been written.
|
|
|
|
/// @brief The bytecode version number
|
2006-11-08 21:27:54 +00:00
|
|
|
const unsigned BCVersionNum = 7;
|
2004-08-17 07:45:14 +00:00
|
|
|
|
2002-07-23 19:56:44 +00:00
|
|
|
static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
|
|
|
|
|
2006-12-06 17:46:33 +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
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== Output Primitives ===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// output - If a position is specified, it must be in the valid portion of the
|
2005-04-21 21:48:46 +00:00
|
|
|
// string... note that this should be inlined always so only the relevant IF
|
2004-07-25 18:07:36 +00:00
|
|
|
// body should be included.
|
|
|
|
inline void BytecodeWriter::output(unsigned i, int pos) {
|
|
|
|
if (pos == -1) { // Be endian clean, little endian is our friend
|
2005-04-21 21:48:46 +00:00
|
|
|
Out.push_back((unsigned char)i);
|
2004-07-25 18:07:36 +00:00
|
|
|
Out.push_back((unsigned char)(i >> 8));
|
|
|
|
Out.push_back((unsigned char)(i >> 16));
|
|
|
|
Out.push_back((unsigned char)(i >> 24));
|
|
|
|
} else {
|
|
|
|
Out[pos ] = (unsigned char)i;
|
|
|
|
Out[pos+1] = (unsigned char)(i >> 8);
|
|
|
|
Out[pos+2] = (unsigned char)(i >> 16);
|
|
|
|
Out[pos+3] = (unsigned char)(i >> 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output(int i) {
|
|
|
|
output((unsigned)i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// output_vbr - Output an unsigned value, by using the least number of bytes
|
|
|
|
/// possible. This is useful because many of our "infinite" values are really
|
|
|
|
/// very small most of the time; but can be large a few times.
|
2005-04-21 21:48:46 +00:00
|
|
|
/// Data format used: If you read a byte with the high bit set, use the low
|
|
|
|
/// seven bits as data and then read another byte.
|
2004-07-25 18:07:36 +00:00
|
|
|
inline void BytecodeWriter::output_vbr(uint64_t i) {
|
|
|
|
while (1) {
|
|
|
|
if (i < 0x80) { // done?
|
|
|
|
Out.push_back((unsigned char)i); // We know the high bit is clear...
|
|
|
|
return;
|
|
|
|
}
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// Nope, we are bigger than a character, output the next 7 bits and set the
|
|
|
|
// high bit to say that there is more coming...
|
|
|
|
Out.push_back(0x80 | ((unsigned char)i & 0x7F));
|
|
|
|
i >>= 7; // Shift out 7 bits now...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_vbr(unsigned i) {
|
|
|
|
while (1) {
|
|
|
|
if (i < 0x80) { // done?
|
|
|
|
Out.push_back((unsigned char)i); // We know the high bit is clear...
|
|
|
|
return;
|
|
|
|
}
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// Nope, we are bigger than a character, output the next 7 bits and set the
|
|
|
|
// high bit to say that there is more coming...
|
|
|
|
Out.push_back(0x80 | ((unsigned char)i & 0x7F));
|
|
|
|
i >>= 7; // Shift out 7 bits now...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_typeid(unsigned i) {
|
|
|
|
if (i <= 0x00FFFFFF)
|
|
|
|
this->output_vbr(i);
|
|
|
|
else {
|
|
|
|
this->output_vbr(0x00FFFFFF);
|
|
|
|
this->output_vbr(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_vbr(int64_t i) {
|
2005-04-21 21:48:46 +00:00
|
|
|
if (i < 0)
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
|
|
|
|
else
|
|
|
|
output_vbr((uint64_t)i << 1); // Low order bit is clear.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_vbr(int i) {
|
2005-04-21 21:48:46 +00:00
|
|
|
if (i < 0)
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
|
|
|
|
else
|
|
|
|
output_vbr((unsigned)i << 1); // Low order bit is clear.
|
|
|
|
}
|
|
|
|
|
2004-08-17 07:45:14 +00:00
|
|
|
inline void BytecodeWriter::output(const std::string &s) {
|
2004-07-25 18:07:36 +00:00
|
|
|
unsigned Len = s.length();
|
2006-07-28 22:07:54 +00:00
|
|
|
output_vbr(Len); // Strings may have an arbitrary length.
|
2004-07-25 18:07:36 +00:00
|
|
|
Out.insert(Out.end(), s.begin(), s.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
|
|
|
|
Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_float(float& FloatVal) {
|
|
|
|
/// FIXME: This isn't optimal, it has size problems on some platforms
|
|
|
|
/// where FP is not IEEE.
|
2005-08-17 19:34:49 +00:00
|
|
|
uint32_t i = FloatToBits(FloatVal);
|
2006-07-28 22:07:54 +00:00
|
|
|
Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
|
2005-08-17 19:34:49 +00:00
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void BytecodeWriter::output_double(double& DoubleVal) {
|
|
|
|
/// FIXME: This isn't optimal, it has size problems on some platforms
|
|
|
|
/// where FP is not IEEE.
|
2005-08-17 19:34:49 +00:00
|
|
|
uint64_t i = DoubleToBits(DoubleVal);
|
2006-07-28 22:07:54 +00:00
|
|
|
Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
|
2005-08-17 19:34:49 +00:00
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF));
|
|
|
|
Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
2005-11-12 18:34:09 +00:00
|
|
|
inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter &w,
|
|
|
|
bool elideIfEmpty, bool hasLongFormat)
|
2004-07-25 18:07:36 +00:00
|
|
|
: Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
|
|
|
|
|
|
|
|
if (HasLongFormat) {
|
|
|
|
w.output(ID);
|
|
|
|
w.output(0U); // For length in long format
|
|
|
|
} else {
|
|
|
|
w.output(0U); /// Place holder for ID and length for this block
|
|
|
|
}
|
|
|
|
Loc = w.size();
|
|
|
|
}
|
|
|
|
|
2004-10-14 01:35:17 +00:00
|
|
|
inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
|
2005-04-21 21:48:46 +00:00
|
|
|
// of scope...
|
2004-07-25 18:07:36 +00:00
|
|
|
if (Loc == Writer.size() && ElideIfEmpty) {
|
|
|
|
// If the block is empty, and we are allowed to, do not emit the block at
|
|
|
|
// all!
|
|
|
|
Writer.resize(Writer.size()-(HasLongFormat?8:4));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasLongFormat)
|
|
|
|
Writer.output(unsigned(Writer.size()-Loc), int(Loc-4));
|
|
|
|
else
|
|
|
|
Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4));
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== Constant Output ===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void BytecodeWriter::outputType(const Type *T) {
|
|
|
|
output_vbr((unsigned)T->getTypeID());
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// 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->getTypeID()) { // Handle derived types now.
|
|
|
|
case Type::FunctionTyID: {
|
|
|
|
const FunctionType *MT = cast<FunctionType>(T);
|
|
|
|
int Slot = Table.getSlot(MT->getReturnType());
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
|
|
|
|
// Output the number of arguments to function (+1 if varargs):
|
|
|
|
output_vbr((unsigned)MT->getNumParams()+MT->isVarArg());
|
|
|
|
|
|
|
|
// Output all of the arguments...
|
|
|
|
FunctionType::param_iterator I = MT->param_begin();
|
|
|
|
for (; I != MT->param_end(); ++I) {
|
|
|
|
Slot = Table.getSlot(*I);
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate list with VoidTy if we are a varargs function...
|
|
|
|
if (MT->isVarArg())
|
|
|
|
output_typeid((unsigned)Type::VoidTyID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
const ArrayType *AT = cast<ArrayType>(T);
|
|
|
|
int Slot = Table.getSlot(AT->getElementType());
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
output_vbr(AT->getNumElements());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-08-20 06:00:58 +00:00
|
|
|
case Type::PackedTyID: {
|
|
|
|
const PackedType *PT = cast<PackedType>(T);
|
|
|
|
int Slot = Table.getSlot(PT->getElementType());
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
output_vbr(PT->getNumElements());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
case Type::StructTyID: {
|
|
|
|
const StructType *ST = cast<StructType>(T);
|
|
|
|
|
|
|
|
// Output all of the element types...
|
|
|
|
for (StructType::element_iterator I = ST->element_begin(),
|
|
|
|
E = ST->element_end(); I != E; ++I) {
|
|
|
|
int Slot = Table.getSlot(*I);
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate list with VoidTy
|
|
|
|
output_typeid((unsigned)Type::VoidTyID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PointerTyID: {
|
|
|
|
const PointerType *PT = cast<PointerType>(T);
|
|
|
|
int Slot = Table.getSlot(PT->getElementType());
|
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-10-14 01:35:17 +00:00
|
|
|
case Type::OpaqueTyID:
|
2004-07-25 18:07:36 +00:00
|
|
|
// No need to emit anything, just the count of opaque types is enough.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
|
|
|
<< " Type '" << T->getDescription() << "'\n";
|
2004-07-25 18:07:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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
|
|
|
|
// a ConstantExpr can be of any type, and has no explicit value.
|
2005-04-21 21:48:46 +00:00
|
|
|
//
|
2004-07-25 18:07:36 +00:00
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
|
|
|
|
// FIXME: Encoding of constant exprs could be much more compact!
|
|
|
|
assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(CE->getNumOperands() != 1 || CE->isCast());
|
2004-10-16 18:18:16 +00:00
|
|
|
output_vbr(1+CE->getNumOperands()); // flags as an expr
|
2006-10-20 07:07:24 +00:00
|
|
|
output_vbr(CE->getOpcode()); // Put out the CE op code
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
|
|
|
|
int Slot = Table.getSlot(*OI);
|
|
|
|
assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
|
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
Slot = Table.getSlot((*OI)->getType());
|
|
|
|
output_typeid((unsigned)Slot);
|
|
|
|
}
|
2006-12-04 05:23:49 +00:00
|
|
|
if (CE->isCompare())
|
|
|
|
output_vbr((unsigned)CE->getPredicate());
|
2004-07-25 18:07:36 +00:00
|
|
|
return;
|
2004-10-16 18:18:16 +00:00
|
|
|
} else if (isa<UndefValue>(CPV)) {
|
|
|
|
output_vbr(1U); // 1 -> UndefValue constant.
|
|
|
|
return;
|
2004-07-25 18:07:36 +00:00
|
|
|
} else {
|
2006-10-20 07:07:24 +00:00
|
|
|
output_vbr(0U); // flag as not a ConstantExpr (i.e. 0 operands)
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
switch (CPV->getType()->getTypeID()) {
|
|
|
|
case Type::BoolTyID: // Boolean Types
|
|
|
|
if (cast<ConstantBool>(CPV)->getValue())
|
|
|
|
output_vbr(1U);
|
|
|
|
else
|
|
|
|
output_vbr(0U);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::UByteTyID: // Unsigned integer types...
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID:
|
|
|
|
case Type::ULongTyID:
|
2006-10-20 07:07:24 +00:00
|
|
|
output_vbr(cast<ConstantInt>(CPV)->getZExtValue());
|
2004-07-25 18:07:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::SByteTyID: // Signed integer types...
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID:
|
|
|
|
case Type::LongTyID:
|
2006-10-20 07:07:24 +00:00
|
|
|
output_vbr(cast<ConstantInt>(CPV)->getSExtValue());
|
2004-07-25 18:07:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
const ConstantArray *CPA = cast<ConstantArray>(CPV);
|
|
|
|
assert(!CPA->isString() && "Constant strings should be handled specially!");
|
|
|
|
|
2004-08-04 08:44:43 +00:00
|
|
|
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) {
|
2004-07-25 18:07:36 +00:00
|
|
|
int Slot = Table.getSlot(CPA->getOperand(i));
|
|
|
|
assert(Slot != -1 && "Constant used but not available!!");
|
2004-08-20 06:00:58 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PackedTyID: {
|
|
|
|
const ConstantPacked *CP = cast<ConstantPacked>(CPV);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
|
|
|
|
int Slot = Table.getSlot(CP->getOperand(i));
|
|
|
|
assert(Slot != -1 && "Constant used but not available!!");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
|
|
|
const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
|
|
|
|
|
2004-08-04 08:44:43 +00:00
|
|
|
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) {
|
|
|
|
int Slot = Table.getSlot(CPS->getOperand(i));
|
2004-07-25 18:07:36 +00:00
|
|
|
assert(Slot != -1 && "Constant used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PointerTyID:
|
|
|
|
assert(0 && "No non-null, non-constant-expr constants allowed!");
|
|
|
|
abort();
|
|
|
|
|
|
|
|
case Type::FloatTyID: { // Floating point types...
|
|
|
|
float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
|
|
|
|
output_float(Tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::DoubleTyID: {
|
|
|
|
double Tmp = cast<ConstantFP>(CPV)->getValue();
|
|
|
|
output_double(Tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:48:46 +00:00
|
|
|
case Type::VoidTyID:
|
2004-07-25 18:07:36 +00:00
|
|
|
case Type::LabelTyID:
|
|
|
|
default:
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
|
|
|
<< " type '" << *CPV->getType() << "'\n";
|
2004-07-25 18:07:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-01-25 23:08:15 +00:00
|
|
|
/// outputInlineAsm - InlineAsm's get emitted to the constant pool, so they can
|
|
|
|
/// be shared by multiple uses.
|
|
|
|
void BytecodeWriter::outputInlineAsm(const InlineAsm *IA) {
|
|
|
|
// Output a marker, so we know when we have one one parsing the constant pool.
|
|
|
|
// Note that this encoding is 5 bytes: not very efficient for a marker. Since
|
|
|
|
// unique inline asms are rare, this should hardly matter.
|
|
|
|
output_vbr(~0U);
|
|
|
|
|
|
|
|
output(IA->getAsmString());
|
|
|
|
output(IA->getConstraintString());
|
|
|
|
output_vbr(unsigned(IA->hasSideEffects()));
|
|
|
|
}
|
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
void BytecodeWriter::outputConstantStrings() {
|
|
|
|
SlotCalculator::string_iterator I = Table.string_begin();
|
|
|
|
SlotCalculator::string_iterator E = Table.string_end();
|
|
|
|
if (I == E) return; // No strings to emit
|
|
|
|
|
|
|
|
// If we have != 0 strings to emit, output them now. Strings are emitted into
|
|
|
|
// the 'void' type plane.
|
|
|
|
output_vbr(unsigned(E-I));
|
|
|
|
output_typeid(Type::VoidTyID);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// Emit all of the strings.
|
|
|
|
for (I = Table.string_begin(); I != E; ++I) {
|
|
|
|
const ConstantArray *Str = *I;
|
|
|
|
int Slot = Table.getSlot(Str->getType());
|
|
|
|
assert(Slot != -1 && "Constant string of unknown type?");
|
|
|
|
output_typeid((unsigned)Slot);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// Now that we emitted the type (which indicates the size of the string),
|
|
|
|
// emit all of the characters.
|
|
|
|
std::string Val = Str->getAsString();
|
|
|
|
output_data(Val.c_str(), Val.c_str()+Val.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== Instruction Output ===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-02-27 06:18:25 +00:00
|
|
|
// outputInstructionFormat0 - Output those weird instructions that have a large
|
2005-05-06 22:34:01 +00:00
|
|
|
// number of operands or have large operands themselves.
|
2004-07-25 18:07:36 +00:00
|
|
|
//
|
|
|
|
// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
|
|
|
|
//
|
2004-10-14 01:46:07 +00:00
|
|
|
void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
|
|
|
|
unsigned Opcode,
|
|
|
|
const SlotCalculator &Table,
|
|
|
|
unsigned Type) {
|
2004-07-25 18:07:36 +00:00
|
|
|
// Opcode must have top two bits clear...
|
|
|
|
output_vbr(Opcode << 2); // Instruction Opcode ID
|
|
|
|
output_typeid(Type); // Result type
|
|
|
|
|
|
|
|
unsigned NumArgs = I->getNumOperands();
|
2006-12-06 04:27:07 +00:00
|
|
|
output_vbr(NumArgs + (isa<CastInst>(I) || isa<InvokeInst>(I) ||
|
|
|
|
isa<CmpInst>(I) || isa<VAArgInst>(I) || Opcode == 58));
|
2004-07-25 18:07:36 +00:00
|
|
|
|
|
|
|
if (!isa<GetElementPtrInst>(&I)) {
|
|
|
|
for (unsigned i = 0; i < NumArgs; ++i) {
|
|
|
|
int Slot = Table.getSlot(I->getOperand(i));
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
|
|
|
|
int Slot = Table.getSlot(I->getType());
|
|
|
|
assert(Slot != -1 && "Cast return type unknown?");
|
|
|
|
output_typeid((unsigned)Slot);
|
2006-12-06 04:27:07 +00:00
|
|
|
} else if (isa<CmpInst>(I)) {
|
|
|
|
output_vbr(unsigned(cast<CmpInst>(I)->getPredicate()));
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (isa<InvokeInst>(I)) {
|
2005-05-06 22:34:01 +00:00
|
|
|
output_vbr(cast<InvokeInst>(I)->getCallingConv());
|
|
|
|
} else if (Opcode == 58) { // Call escape sequence
|
|
|
|
output_vbr((cast<CallInst>(I)->getCallingConv() << 1) |
|
2005-05-07 02:44:04 +00:00
|
|
|
unsigned(cast<CallInst>(I)->isTailCall()));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int Slot = Table.getSlot(I->getOperand(0));
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(unsigned(Slot));
|
|
|
|
|
|
|
|
// We need to encode the type of sequential type indices into their slot #
|
|
|
|
unsigned Idx = 1;
|
|
|
|
for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
|
|
|
|
Idx != NumArgs; ++TI, ++Idx) {
|
|
|
|
Slot = Table.getSlot(I->getOperand(Idx));
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
if (isa<SequentialType>(*TI)) {
|
|
|
|
unsigned IdxId;
|
|
|
|
switch (I->getOperand(Idx)->getType()->getTypeID()) {
|
|
|
|
default: assert(0 && "Unknown index type!");
|
|
|
|
case Type::UIntTyID: IdxId = 0; break;
|
|
|
|
case Type::IntTyID: IdxId = 1; break;
|
|
|
|
case Type::ULongTyID: IdxId = 2; break;
|
|
|
|
case Type::LongTyID: IdxId = 3; break;
|
|
|
|
}
|
|
|
|
Slot = (Slot << 2) | IdxId;
|
|
|
|
}
|
|
|
|
output_vbr(unsigned(Slot));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
|
|
|
|
// This are more annoying than most because the signature of the call does not
|
|
|
|
// tell us anything about the types of the arguments in the varargs portion.
|
|
|
|
// Because of this, we encode (as type 0) all of the argument types explicitly
|
|
|
|
// before the argument value. This really sucks, but you shouldn't be using
|
|
|
|
// varargs functions in your code! *death to printf*!
|
|
|
|
//
|
|
|
|
// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
|
|
|
|
//
|
2005-04-21 21:48:46 +00:00
|
|
|
void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
|
|
|
|
unsigned Opcode,
|
|
|
|
const SlotCalculator &Table,
|
|
|
|
unsigned Type) {
|
2004-07-25 18:07:36 +00:00
|
|
|
assert(isa<CallInst>(I) || isa<InvokeInst>(I));
|
|
|
|
// Opcode must have top two bits clear...
|
|
|
|
output_vbr(Opcode << 2); // Instruction Opcode ID
|
|
|
|
output_typeid(Type); // Result type (varargs type)
|
|
|
|
|
|
|
|
const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
|
|
|
|
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
|
|
|
|
unsigned NumParams = FTy->getNumParams();
|
|
|
|
|
|
|
|
unsigned NumFixedOperands;
|
|
|
|
if (isa<CallInst>(I)) {
|
|
|
|
// Output an operand for the callee and each fixed argument, then two for
|
|
|
|
// each variable argument.
|
|
|
|
NumFixedOperands = 1+NumParams;
|
|
|
|
} else {
|
|
|
|
assert(isa<InvokeInst>(I) && "Not call or invoke??");
|
|
|
|
// Output an operand for the callee and destinations, then two for each
|
|
|
|
// variable argument.
|
|
|
|
NumFixedOperands = 3+NumParams;
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
output_vbr(2 * I->getNumOperands()-NumFixedOperands +
|
|
|
|
unsigned(Opcode == 58 || isa<InvokeInst>(I)));
|
2004-07-25 18:07:36 +00:00
|
|
|
|
|
|
|
// The type for the function has already been emitted in the type field of the
|
|
|
|
// instruction. Just emit the slot # now.
|
|
|
|
for (unsigned i = 0; i != NumFixedOperands; ++i) {
|
|
|
|
int Slot = Table.getSlot(I->getOperand(i));
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
|
|
|
|
// Output Arg Type ID
|
|
|
|
int Slot = Table.getSlot(I->getOperand(i)->getType());
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid((unsigned)Slot);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
// Output arg ID itself
|
|
|
|
Slot = Table.getSlot(I->getOperand(i));
|
2005-04-21 21:48:46 +00:00
|
|
|
assert(Slot >= 0 && "No slot number for value!?!?");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
|
|
|
}
|
2006-05-26 18:42:34 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
if (isa<InvokeInst>(I)) {
|
|
|
|
// Emit the tail call/calling conv for invoke instructions
|
|
|
|
output_vbr(cast<InvokeInst>(I)->getCallingConv());
|
|
|
|
} else if (Opcode == 58) {
|
2006-05-26 18:42:34 +00:00
|
|
|
const CallInst *CI = cast<CallInst>(I);
|
|
|
|
output_vbr((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
|
|
|
|
}
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// outputInstructionFormat1 - Output one operand instructions, knowing that no
|
|
|
|
// operand index is >= 2^12.
|
|
|
|
//
|
2005-04-21 21:48:46 +00:00
|
|
|
inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
|
|
|
|
unsigned Opcode,
|
|
|
|
unsigned *Slots,
|
|
|
|
unsigned Type) {
|
2004-07-25 18:07:36 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 01-00: Opcode type, fixed to 1.
|
|
|
|
// 07-02: Opcode
|
|
|
|
// 19-08: Resulting type plane
|
|
|
|
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
|
|
|
|
//
|
2004-10-14 01:46:07 +00:00
|
|
|
output(1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// outputInstructionFormat2 - Output two operand instructions, knowing that no
|
|
|
|
// operand index is >= 2^8.
|
|
|
|
//
|
2005-04-21 21:48:46 +00:00
|
|
|
inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
|
|
|
|
unsigned Opcode,
|
|
|
|
unsigned *Slots,
|
|
|
|
unsigned Type) {
|
2004-07-25 18:07:36 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 01-00: Opcode type, fixed to 2.
|
|
|
|
// 07-02: Opcode
|
|
|
|
// 15-08: Resulting type plane
|
|
|
|
// 23-16: Operand #1
|
2005-04-21 21:48:46 +00:00
|
|
|
// 31-24: Operand #2
|
2004-07-25 18:07:36 +00:00
|
|
|
//
|
2004-10-14 01:46:07 +00:00
|
|
|
output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// outputInstructionFormat3 - Output three operand instructions, knowing that no
|
|
|
|
// operand index is >= 2^6.
|
|
|
|
//
|
2005-04-21 21:48:46 +00:00
|
|
|
inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
|
2004-07-25 18:07:36 +00:00
|
|
|
unsigned Opcode,
|
2005-04-21 21:48:46 +00:00
|
|
|
unsigned *Slots,
|
|
|
|
unsigned Type) {
|
2004-07-25 18:07:36 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 01-00: Opcode type, fixed to 3.
|
|
|
|
// 07-02: Opcode
|
|
|
|
// 13-08: Resulting type plane
|
|
|
|
// 19-14: Operand #1
|
|
|
|
// 25-20: Operand #2
|
|
|
|
// 31-26: Operand #3
|
|
|
|
//
|
2004-10-14 01:46:07 +00:00
|
|
|
output(3 | (Opcode << 2) | (Type << 8) |
|
2004-10-14 01:57:28 +00:00
|
|
|
(Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26));
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputInstruction(const Instruction &I) {
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(I.getOpcode() < 57 && "Opcode too big???");
|
2004-07-25 18:07:36 +00:00
|
|
|
unsigned Opcode = I.getOpcode();
|
|
|
|
unsigned NumOperands = I.getNumOperands();
|
|
|
|
|
2005-05-06 06:13:34 +00:00
|
|
|
// Encode 'tail call' as 61, 'volatile load' as 62, and 'volatile store' as
|
|
|
|
// 63.
|
2005-05-06 22:34:01 +00:00
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
|
|
|
|
if (CI->getCallingConv() == CallingConv::C) {
|
|
|
|
if (CI->isTailCall())
|
|
|
|
Opcode = 61; // CCC + Tail Call
|
|
|
|
else
|
|
|
|
; // Opcode = Instruction::Call
|
|
|
|
} else if (CI->getCallingConv() == CallingConv::Fast) {
|
|
|
|
if (CI->isTailCall())
|
|
|
|
Opcode = 59; // FastCC + TailCall
|
|
|
|
else
|
|
|
|
Opcode = 60; // FastCC + Not Tail Call
|
|
|
|
} else {
|
|
|
|
Opcode = 58; // Call escape sequence.
|
|
|
|
}
|
|
|
|
} else if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) {
|
2004-07-25 18:07:36 +00:00
|
|
|
Opcode = 62;
|
2005-05-06 22:34:01 +00:00
|
|
|
} else if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) {
|
2004-07-25 18:07:36 +00:00
|
|
|
Opcode = 63;
|
2005-05-06 22:34:01 +00:00
|
|
|
}
|
2004-07-25 18:07:36 +00:00
|
|
|
|
|
|
|
// 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
|
2005-04-21 21:48:46 +00:00
|
|
|
// we take the type of the instruction itself.
|
2004-07-25 18:07:36 +00:00
|
|
|
//
|
|
|
|
const Type *Ty;
|
|
|
|
switch (I.getOpcode()) {
|
|
|
|
case Instruction::Select:
|
|
|
|
case Instruction::Malloc:
|
|
|
|
case Instruction::Alloca:
|
|
|
|
Ty = I.getType(); // These ALWAYS want to encode the return type
|
|
|
|
break;
|
|
|
|
case Instruction::Store:
|
|
|
|
Ty = I.getOperand(1)->getType(); // Encode the pointer type...
|
|
|
|
assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
|
|
|
|
break;
|
|
|
|
default: // Otherwise use the default behavior...
|
|
|
|
Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Type;
|
|
|
|
int Slot = Table.getSlot(Ty);
|
|
|
|
assert(Slot != -1 && "Type not available!!?!");
|
|
|
|
Type = (unsigned)Slot;
|
|
|
|
|
|
|
|
// Varargs calls and invokes are encoded entirely different from any other
|
|
|
|
// instructions.
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(&I)){
|
|
|
|
const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
|
|
|
|
if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
|
|
|
|
outputInstrVarArgsCall(CI, Opcode, Table, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
|
|
|
|
const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
|
|
|
|
if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
|
|
|
|
outputInstrVarArgsCall(II, Opcode, Table, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumOperands <= 3) {
|
|
|
|
// Make sure that we take the type number into consideration. We don't want
|
|
|
|
// to overflow the field size for the instruction format we select.
|
|
|
|
//
|
|
|
|
unsigned MaxOpSlot = Type;
|
|
|
|
unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
for (unsigned i = 0; i != NumOperands; ++i) {
|
|
|
|
int slot = Table.getSlot(I.getOperand(i));
|
|
|
|
assert(slot != -1 && "Broken bytecode!");
|
|
|
|
if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
|
|
|
|
Slots[i] = unsigned(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the special cases for various instructions...
|
|
|
|
if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
|
|
|
|
// Cast has to encode the destination type as the second argument in the
|
|
|
|
// packet, or else we won't know what type to cast to!
|
|
|
|
Slots[1] = Table.getSlot(I.getType());
|
|
|
|
assert(Slots[1] != ~0U && "Cast return type unknown?");
|
|
|
|
if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
|
|
|
|
NumOperands++;
|
2005-11-05 22:08:14 +00:00
|
|
|
} else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
|
|
|
|
assert(NumOperands == 1 && "Bogus allocation!");
|
|
|
|
if (AI->getAlignment()) {
|
|
|
|
Slots[1] = Log2_32(AI->getAlignment())+1;
|
|
|
|
if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
|
|
|
|
NumOperands = 2;
|
|
|
|
}
|
2006-12-03 06:28:54 +00:00
|
|
|
} else if (isa<ICmpInst>(I) || isa<FCmpInst>(I)) {
|
|
|
|
// We need to encode the compare instruction's predicate as the third
|
|
|
|
// operand. Its not really a slot, but we don't want to break the
|
|
|
|
// instruction format for these instructions.
|
|
|
|
NumOperands++;
|
|
|
|
assert(NumOperands == 3 && "CmpInst with wrong number of operands?");
|
|
|
|
Slots[2] = unsigned(cast<CmpInst>(&I)->getPredicate());
|
|
|
|
if (Slots[2] > MaxOpSlot)
|
|
|
|
MaxOpSlot = Slots[2];
|
2004-07-25 18:07:36 +00:00
|
|
|
} else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
|
|
|
|
// We need to encode the type of sequential type indices into their slot #
|
|
|
|
unsigned Idx = 1;
|
|
|
|
for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
|
|
|
|
I != E; ++I, ++Idx)
|
|
|
|
if (isa<SequentialType>(*I)) {
|
|
|
|
unsigned IdxId;
|
|
|
|
switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
|
|
|
|
default: assert(0 && "Unknown index type!");
|
|
|
|
case Type::UIntTyID: IdxId = 0; break;
|
|
|
|
case Type::IntTyID: IdxId = 1; break;
|
|
|
|
case Type::ULongTyID: IdxId = 2; break;
|
|
|
|
case Type::LongTyID: IdxId = 3; break;
|
|
|
|
}
|
|
|
|
Slots[Idx] = (Slots[Idx] << 2) | IdxId;
|
|
|
|
if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
|
|
|
|
}
|
2005-05-06 22:34:01 +00:00
|
|
|
} else if (Opcode == 58) {
|
|
|
|
// If this is the escape sequence for call, emit the tailcall/cc info.
|
|
|
|
const CallInst &CI = cast<CallInst>(I);
|
|
|
|
++NumOperands;
|
2006-05-19 21:57:37 +00:00
|
|
|
if (NumOperands <= 3) {
|
|
|
|
Slots[NumOperands-1] =
|
|
|
|
(CI.getCallingConv() << 1)|unsigned(CI.isTailCall());
|
2005-05-06 22:34:01 +00:00
|
|
|
if (Slots[NumOperands-1] > MaxOpSlot)
|
|
|
|
MaxOpSlot = Slots[NumOperands-1];
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (isa<InvokeInst>(I)) {
|
2005-05-06 22:34:01 +00:00
|
|
|
// Invoke escape seq has at least 4 operands to encode.
|
|
|
|
++NumOperands;
|
2004-07-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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, Opcode, Slots, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (MaxOpSlot < (1 << 8)) {
|
|
|
|
outputInstructionFormat2(&I, Opcode, Slots, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if (MaxOpSlot < (1 << 6)) {
|
|
|
|
outputInstructionFormat3(&I, Opcode, Slots, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we weren't handled before here, we either have a large number of
|
|
|
|
// operands or a large operand index that we are referring to.
|
|
|
|
outputInstructionFormat0(&I, Opcode, Table, Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== Block Output ===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-04-21 21:48:46 +00:00
|
|
|
BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
|
2004-05-26 07:37:11 +00:00
|
|
|
: Out(o), Table(M) {
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-14 23:36:54 +00:00
|
|
|
// Emit the signature...
|
2006-07-28 22:07:54 +00:00
|
|
|
static const unsigned char *Sig = (const unsigned char*)"llvm";
|
2004-07-25 18:07:36 +00:00
|
|
|
output_data(Sig, Sig+4);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Emit the top level CLASS block.
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeBlock ModuleBlock(BytecodeFormat::ModuleBlockID, *this, false, true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
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
|
|
|
|
2004-10-16 18:18:16 +00:00
|
|
|
// Output the version identifier and other information.
|
2005-04-21 21:48:46 +00:00
|
|
|
unsigned Version = (BCVersionNum << 4) |
|
2004-08-17 07:45:14 +00:00
|
|
|
(unsigned)isBigEndian | (hasLongPointers << 1) |
|
2005-04-21 21:48:46 +00:00
|
|
|
(hasNoEndianness << 2) |
|
2004-08-17 07:45:14 +00:00
|
|
|
(hasNoPointerSize << 3);
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(Version);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-07-04 11:45:47 +00:00
|
|
|
// The Global type plane comes first
|
2003-03-19 20:56:46 +00:00
|
|
|
{
|
2006-07-28 22:07:54 +00:00
|
|
|
BytecodeBlock CPool(BytecodeFormat::GlobalTypePlaneBlockID, *this);
|
|
|
|
outputTypes(Type::FirstDerivedTyID);
|
2003-03-19 20:56:46 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2004-10-14 01:46:07 +00:00
|
|
|
void BytecodeWriter::outputTypes(unsigned TypeNum) {
|
2004-07-04 11:45:47 +00:00
|
|
|
// 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 Type*>& Types = Table.getTypes();
|
|
|
|
assert(!Types.empty() && "No types at all?");
|
|
|
|
assert(TypeNum <= Types.size() && "Invalid TypeNo index");
|
|
|
|
|
|
|
|
unsigned NumEntries = Types.size() - TypeNum;
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-04 11:45:47 +00:00
|
|
|
// Output type header: [num entries]
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(NumEntries);
|
2004-07-04 11:45:47 +00:00
|
|
|
|
|
|
|
for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i)
|
|
|
|
outputType(Types[i]);
|
|
|
|
}
|
|
|
|
|
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.
|
2005-04-21 21:48:46 +00:00
|
|
|
//
|
2002-07-14 23:07:51 +00:00
|
|
|
void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
|
|
|
|
&Plane, unsigned StartNo) {
|
|
|
|
unsigned ValNo = StartNo;
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-01-14 23:36:54 +00:00
|
|
|
// Scan through and ignore function arguments, global values, and constant
|
|
|
|
// strings.
|
|
|
|
for (; ValNo < Plane.size() &&
|
|
|
|
(isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
|
|
|
|
(isa<ConstantArray>(Plane[ValNo]) &&
|
|
|
|
cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
|
2002-07-14 23:07:51 +00:00
|
|
|
/*empty*/;
|
|
|
|
|
|
|
|
unsigned NC = ValNo; // Number of constants
|
2006-01-25 23:08:15 +00:00
|
|
|
for (; NC < Plane.size() && (isa<Constant>(Plane[NC]) ||
|
|
|
|
isa<InlineAsm>(Plane[NC])); NC++)
|
2002-07-14 23:07:51 +00:00
|
|
|
/*empty*/;
|
|
|
|
NC -= ValNo; // Convert from index into count
|
|
|
|
if (NC == 0) return; // Skip empty type planes...
|
|
|
|
|
2004-01-14 16:54:21 +00:00
|
|
|
// FIXME: Most slabs only have 1 or 2 entries! We should encode this much
|
|
|
|
// more compactly.
|
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
// Put out type header: [num entries][type id number]
|
2002-07-14 23:07:51 +00:00
|
|
|
//
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(NC);
|
2002-07-14 23:07:51 +00:00
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
// Put out 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!!");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid((unsigned)Slot);
|
2002-07-14 23:07:51 +00:00
|
|
|
|
|
|
|
for (unsigned i = ValNo; i < ValNo+NC; ++i) {
|
|
|
|
const Value *V = Plane[i];
|
2006-01-25 23:08:15 +00:00
|
|
|
if (const Constant *C = dyn_cast<Constant>(V))
|
2004-07-18 00:16:21 +00:00
|
|
|
outputConstant(C);
|
2006-01-25 23:08:15 +00:00
|
|
|
else
|
|
|
|
outputInlineAsm(cast<InlineAsm>(V));
|
2002-07-14 23:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-05 22:21:19 +00:00
|
|
|
static inline bool hasNullValue(const Type *Ty) {
|
|
|
|
return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
|
2004-01-17 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
void BytecodeWriter::outputConstants(bool isFunction) {
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeBlock CPool(BytecodeFormat::ConstantPoolBlockID, *this,
|
2004-01-15 21:06:57 +00:00
|
|
|
true /* Elide block if empty */);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
unsigned NumPlanes = Table.getNumPlanes();
|
2003-03-19 20:56:46 +00:00
|
|
|
|
2004-07-18 00:16:21 +00:00
|
|
|
if (isFunction)
|
|
|
|
// Output the type plane before any constants!
|
2004-10-16 18:18:16 +00:00
|
|
|
outputTypes(Table.getModuleTypeLevel());
|
2004-07-18 00:16:21 +00:00
|
|
|
else
|
2004-10-14 01:46:07 +00:00
|
|
|
// Output module-level string constants before any other constants.
|
2004-01-14 23:36:54 +00:00
|
|
|
outputConstantStrings();
|
|
|
|
|
2004-07-04 11:45:47 +00:00
|
|
|
for (unsigned pno = 0; pno != NumPlanes; pno++) {
|
|
|
|
const std::vector<const Value*> &Plane = Table.getPlane(pno);
|
|
|
|
if (!Plane.empty()) { // Skip empty type planes...
|
|
|
|
unsigned ValNo = 0;
|
|
|
|
if (isFunction) // Don't re-emit module constants
|
2004-07-04 11:46:15 +00:00
|
|
|
ValNo += Table.getModuleLevel(pno);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2005-05-05 22:21:19 +00:00
|
|
|
if (hasNullValue(Plane[0]->getType())) {
|
2004-07-04 11:46:15 +00:00
|
|
|
// Skip zero initializer
|
|
|
|
if (ValNo == 0)
|
|
|
|
ValNo = 1;
|
2003-05-22 18:35:38 +00:00
|
|
|
}
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-07-04 11:45:47 +00:00
|
|
|
// Write out constants in the plane
|
|
|
|
outputConstantsInPlane(Plane, ValNo);
|
2003-05-22 18:35:38 +00:00
|
|
|
}
|
2004-07-04 11:45:47 +00:00
|
|
|
}
|
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!");
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::ExternalLinkage: return 0;
|
|
|
|
case GlobalValue::WeakLinkage: return 1;
|
|
|
|
case GlobalValue::AppendingLinkage: return 2;
|
|
|
|
case GlobalValue::InternalLinkage: return 3;
|
|
|
|
case GlobalValue::LinkOnceLinkage: return 4;
|
|
|
|
case GlobalValue::DLLImportLinkage: return 5;
|
|
|
|
case GlobalValue::DLLExportLinkage: return 6;
|
|
|
|
case GlobalValue::ExternalWeakLinkage: return 7;
|
2003-10-16 18:28:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2005-11-12 01:33:40 +00:00
|
|
|
// Give numbers to sections as we encounter them.
|
|
|
|
unsigned SectionIDCounter = 0;
|
|
|
|
std::vector<std::string> SectionNames;
|
|
|
|
std::map<std::string, unsigned> SectionID;
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Output the types for the global variables in the module...
|
2005-05-06 20:27:03 +00:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(),
|
2005-11-06 07:11:04 +00:00
|
|
|
End = M->global_end(); 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
|
|
|
|
2005-11-06 07:11:04 +00:00
|
|
|
assert((I->hasInitializer() || !I->hasInternalLinkage()) &&
|
|
|
|
"Global must have an initializer or have external linkage!");
|
|
|
|
|
2003-10-18 06:30:21 +00:00
|
|
|
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
|
2005-11-06 07:11:04 +00:00
|
|
|
// bit5+ = Slot # for type.
|
2005-11-12 01:33:40 +00:00
|
|
|
bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
|
2005-11-06 07:11:04 +00:00
|
|
|
|
|
|
|
// If we need to use the extension byte, set linkage=3(internal) and
|
|
|
|
// initializer = 0 (impossible!).
|
|
|
|
if (!HasExtensionWord) {
|
|
|
|
unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
|
|
|
|
(I->hasInitializer() << 1) | (unsigned)I->isConstant();
|
|
|
|
output_vbr(oSlot);
|
2006-09-14 18:23:27 +00:00
|
|
|
} else {
|
2005-11-06 07:11:04 +00:00
|
|
|
unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) |
|
|
|
|
(0 << 1) | (unsigned)I->isConstant();
|
|
|
|
output_vbr(oSlot);
|
|
|
|
|
|
|
|
// The extension word has this format: bit 0 = has initializer, bit 1-3 =
|
2005-11-12 01:33:40 +00:00
|
|
|
// linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
|
|
|
|
// bits 10+ = future use.
|
2005-11-12 01:01:50 +00:00
|
|
|
unsigned ExtWord = (unsigned)I->hasInitializer() |
|
|
|
|
(getEncodedLinkage(I) << 1) |
|
2005-11-12 01:33:40 +00:00
|
|
|
((Log2_32(I->getAlignment())+1) << 4) |
|
|
|
|
((unsigned)I->hasSection() << 9);
|
2005-11-06 07:11:04 +00:00
|
|
|
output_vbr(ExtWord);
|
2005-11-12 01:33:40 +00:00
|
|
|
if (I->hasSection()) {
|
|
|
|
// Give section names unique ID's.
|
|
|
|
unsigned &Entry = SectionID[I->getSection()];
|
|
|
|
if (Entry == 0) {
|
|
|
|
Entry = ++SectionIDCounter;
|
|
|
|
SectionNames.push_back(I->getSection());
|
|
|
|
}
|
|
|
|
output_vbr(Entry);
|
|
|
|
}
|
2005-11-06 07:11:04 +00:00
|
|
|
}
|
2001-09-18 04:01:05 +00:00
|
|
|
|
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!");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
2001-09-18 04:01:05 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
}
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid((unsigned)Table.getSlot(Type::VoidTy));
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2004-10-16 18:18:16 +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());
|
2004-10-16 18:18:16 +00:00
|
|
|
assert(Slot != -1 && "Module slot calculator is broken!");
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
|
2005-11-06 07:43:39 +00:00
|
|
|
assert(((Slot << 6) >> 6) == Slot && "Slot # too big!");
|
2005-11-06 07:46:13 +00:00
|
|
|
unsigned CC = I->getCallingConv()+1;
|
|
|
|
unsigned ID = (Slot << 5) | (CC & 15);
|
2005-05-06 20:42:57 +00:00
|
|
|
|
2004-11-15 22:39:49 +00:00
|
|
|
if (I->isExternal()) // If external, we don't have an FunctionInfo block.
|
|
|
|
ID |= 1 << 4;
|
2005-11-06 07:43:39 +00:00
|
|
|
|
2006-09-14 18:23:27 +00:00
|
|
|
if (I->getAlignment() || I->hasSection() || (CC & ~15) != 0 ||
|
|
|
|
(I->isExternal() && I->hasDLLImportLinkage()) ||
|
|
|
|
(I->isExternal() && I->hasExternalWeakLinkage())
|
|
|
|
)
|
2005-11-06 07:43:39 +00:00
|
|
|
ID |= 1 << 31; // Do we need an extension word?
|
|
|
|
|
2004-10-16 18:18:16 +00:00
|
|
|
output_vbr(ID);
|
2005-11-06 07:43:39 +00:00
|
|
|
|
|
|
|
if (ID & (1 << 31)) {
|
|
|
|
// Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling
|
2006-09-14 18:23:27 +00:00
|
|
|
// convention, bit 10 = hasSectionID., bits 11-12 = external linkage type
|
|
|
|
unsigned extLinkage = 0;
|
|
|
|
|
|
|
|
if (I->isExternal()) {
|
|
|
|
if (I->hasDLLImportLinkage()) {
|
|
|
|
extLinkage = 1;
|
|
|
|
} else if (I->hasExternalWeakLinkage()) {
|
|
|
|
extLinkage = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-12 01:33:40 +00:00
|
|
|
ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5) |
|
2006-09-14 18:23:27 +00:00
|
|
|
(I->hasSection() << 10) |
|
|
|
|
((extLinkage & 3) << 11);
|
2005-11-06 07:43:39 +00:00
|
|
|
output_vbr(ID);
|
2005-11-12 01:33:40 +00:00
|
|
|
|
|
|
|
// Give section names unique ID's.
|
|
|
|
if (I->hasSection()) {
|
|
|
|
unsigned &Entry = SectionID[I->getSection()];
|
|
|
|
if (Entry == 0) {
|
|
|
|
Entry = ++SectionIDCounter;
|
|
|
|
SectionNames.push_back(I->getSection());
|
|
|
|
}
|
|
|
|
output_vbr(Entry);
|
|
|
|
}
|
2005-11-06 07:43:39 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-10-16 18:18:16 +00:00
|
|
|
output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5);
|
2004-07-25 18:07:36 +00:00
|
|
|
|
2004-10-16 18:18:16 +00:00
|
|
|
// Emit the list of dependent libraries for the Module.
|
2004-07-25 21:32:02 +00:00
|
|
|
Module::lib_iterator LI = M->lib_begin();
|
|
|
|
Module::lib_iterator LE = M->lib_end();
|
2004-10-16 18:18:16 +00:00
|
|
|
output_vbr(unsigned(LE - LI)); // Emit the number of dependent libraries.
|
|
|
|
for (; LI != LE; ++LI)
|
2004-08-17 07:45:14 +00:00
|
|
|
output(*LI);
|
2004-07-25 18:07:36 +00:00
|
|
|
|
|
|
|
// Output the target triple from the module
|
2004-08-17 07:45:14 +00:00
|
|
|
output(M->getTargetTriple());
|
2005-11-12 01:33:40 +00:00
|
|
|
|
|
|
|
// Emit the table of section names.
|
|
|
|
output_vbr((unsigned)SectionNames.size());
|
|
|
|
for (unsigned i = 0, e = SectionNames.size(); i != e; ++i)
|
|
|
|
output(SectionNames[i]);
|
2006-01-23 23:43:17 +00:00
|
|
|
|
|
|
|
// Output the inline asm string.
|
2006-01-24 04:14:29 +00:00
|
|
|
output(M->getModuleInlineAsm());
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
void BytecodeWriter::outputInstructions(const Function *F) {
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeBlock ILBlock(BytecodeFormat::InstructionListBlockID, *this);
|
2004-01-18 21:08:52 +00:00
|
|
|
for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
|
|
|
|
outputInstruction(*I);
|
|
|
|
}
|
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
void BytecodeWriter::outputFunction(const Function *F) {
|
2004-11-15 21:56:33 +00:00
|
|
|
// If this is an external function, there is nothing else to emit!
|
|
|
|
if (F->isExternal()) return;
|
|
|
|
|
2004-11-15 22:39:49 +00:00
|
|
|
BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
|
|
|
|
output_vbr(getEncodedLinkage(F));
|
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
// Get slot information about the function...
|
|
|
|
Table.incorporateFunction(F);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
if (Table.getCompactionTable().empty()) {
|
|
|
|
// Output information about the constants in the function if the compaction
|
|
|
|
// table is not being used.
|
|
|
|
outputConstants(true);
|
|
|
|
} else {
|
|
|
|
// Otherwise, emit the compaction table.
|
|
|
|
outputCompactionTable();
|
2001-09-07 16:39:41 +00:00
|
|
|
}
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
// Output all of the instructions in the body of the function
|
|
|
|
outputInstructions(F);
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
// If needed, output the symbol table for the function...
|
|
|
|
outputSymbolTable(F->getSymbolTable());
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
Table.purgeFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
|
|
|
|
const std::vector<const Value*> &Plane,
|
|
|
|
unsigned StartNo) {
|
|
|
|
unsigned End = Table.getModuleLevel(PlaneNo);
|
2004-01-20 00:54:06 +00:00
|
|
|
if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit
|
2004-01-18 21:08:52 +00:00
|
|
|
assert(StartNo < End && "Cannot emit negative range!");
|
|
|
|
assert(StartNo < Plane.size() && End <= Plane.size());
|
|
|
|
|
|
|
|
// Do not emit the null initializer!
|
2004-07-04 11:45:47 +00:00
|
|
|
++StartNo;
|
2004-01-18 21:08:52 +00:00
|
|
|
|
2004-01-18 22:35:34 +00:00
|
|
|
// Figure out which encoding to use. By far the most common case we have is
|
|
|
|
// to emit 0-2 entries in a compaction table plane.
|
|
|
|
switch (End-StartNo) {
|
|
|
|
case 0: // Avoid emitting two vbr's if possible.
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((PlaneNo << 2) | End-StartNo);
|
2004-01-18 22:35:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Output the number of things.
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned(End-StartNo) << 2) | 3);
|
|
|
|
output_typeid(PlaneNo); // Emit the type plane this is
|
2004-01-18 22:35:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
for (unsigned i = StartNo; i != End; ++i)
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(Table.getGlobalSlot(Plane[i]));
|
2004-01-18 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:45:47 +00:00
|
|
|
void BytecodeWriter::outputCompactionTypes(unsigned StartNo) {
|
|
|
|
// Get the compaction type table from the slot calculator
|
|
|
|
const std::vector<const Type*> &CTypes = Table.getCompactionTypes();
|
|
|
|
|
|
|
|
// The compaction types may have been uncompactified back to the
|
|
|
|
// global types. If so, we just write an empty table
|
2006-07-28 22:07:54 +00:00
|
|
|
if (CTypes.size() == 0) {
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(0U);
|
2004-07-04 11:45:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(CTypes.size() >= StartNo && "Invalid compaction types start index");
|
|
|
|
|
|
|
|
// Determine how many types to write
|
|
|
|
unsigned NumTypes = CTypes.size() - StartNo;
|
|
|
|
|
|
|
|
// Output the number of types.
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(NumTypes);
|
2004-07-04 11:45:47 +00:00
|
|
|
|
|
|
|
for (unsigned i = StartNo; i < StartNo+NumTypes; ++i)
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid(Table.getGlobalSlot(CTypes[i]));
|
2004-07-04 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2004-01-18 21:08:52 +00:00
|
|
|
void BytecodeWriter::outputCompactionTable() {
|
2004-08-27 00:38:44 +00:00
|
|
|
// Avoid writing the compaction table at all if there is no content.
|
|
|
|
if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID ||
|
|
|
|
(!Table.CompactionTableIsEmpty())) {
|
2005-04-21 21:48:46 +00:00
|
|
|
BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
|
2004-08-27 00:38:44 +00:00
|
|
|
true/*ElideIfEmpty*/);
|
2004-10-14 01:46:07 +00:00
|
|
|
const std::vector<std::vector<const Value*> > &CT =
|
|
|
|
Table.getCompactionTable();
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2004-08-27 00:38:44 +00:00
|
|
|
// First things first, emit the type compaction table if there is one.
|
|
|
|
outputCompactionTypes(Type::FirstDerivedTyID);
|
2004-01-18 21:08:52 +00:00
|
|
|
|
2004-08-27 00:38:44 +00:00
|
|
|
for (unsigned i = 0, e = CT.size(); i != e; ++i)
|
|
|
|
outputCompactionTablePlane(i, CT[i], 0);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
|
2004-01-10 19:56:59 +00:00
|
|
|
// Do not output the Bytecode block for an empty symbol table, it just wastes
|
|
|
|
// space!
|
2004-10-14 01:46:07 +00:00
|
|
|
if (MST.isEmpty()) return;
|
2004-01-10 19:56:59 +00:00
|
|
|
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this,
|
2004-10-14 01:46:07 +00:00
|
|
|
true/*ElideIfEmpty*/);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2005-04-21 21:48:46 +00:00
|
|
|
// Write the number of types
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr(MST.num_types());
|
2004-08-17 02:59:02 +00:00
|
|
|
|
|
|
|
// Write each of the types
|
2004-05-25 17:29:59 +00:00
|
|
|
for (SymbolTable::type_const_iterator TI = MST.type_begin(),
|
2006-07-28 22:07:54 +00:00
|
|
|
TE = MST.type_end(); TI != TE; ++TI) {
|
2004-08-17 02:59:02 +00:00
|
|
|
// Symtab entry:[def slot #][name]
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid((unsigned)Table.getSlot(TI->second));
|
2005-04-21 21:48:46 +00:00
|
|
|
output(TI->first);
|
2004-05-25 17:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now do each of the type planes in order.
|
2005-04-21 21:48:46 +00:00
|
|
|
for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
|
2004-05-25 17:29:59 +00:00
|
|
|
PE = MST.plane_end(); PI != PE; ++PI) {
|
|
|
|
SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
|
|
|
|
SymbolTable::value_const_iterator End = MST.value_end(PI->first);
|
2001-06-06 20:29:01 +00:00
|
|
|
int Slot;
|
2005-04-21 21:48:46 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (I == End) continue; // Don't mess with an absent type...
|
|
|
|
|
2004-08-17 02:59:02 +00:00
|
|
|
// Write the number of values in this plane
|
2005-03-07 02:59:36 +00:00
|
|
|
output_vbr((unsigned)PI->second.size());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-08-17 02:59:02 +00:00
|
|
|
// Write the slot number of the type for this plane
|
2004-05-25 17:29:59 +00:00
|
|
|
Slot = Table.getSlot(PI->first);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type in symtab, but not in table!");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_typeid((unsigned)Slot);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-08-17 02:59:02 +00:00
|
|
|
// Write each of the values in this plane
|
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!!");
|
2004-07-25 18:07:36 +00:00
|
|
|
output_vbr((unsigned)Slot);
|
2004-08-17 07:45:14 +00:00
|
|
|
output(I->first);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
void llvm::WriteBytecodeToFile(const Module *M, OStream &Out,
|
2006-07-28 22:07:54 +00:00
|
|
|
bool compress) {
|
2004-07-25 18:07:36 +00:00
|
|
|
assert(M && "You can't write a null module!!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2006-06-07 23:18:34 +00:00
|
|
|
// Make sure that std::cout is put into binary mode for systems
|
|
|
|
// that care.
|
2006-12-07 01:30:32 +00:00
|
|
|
if (Out == cout)
|
2006-06-07 23:18:34 +00:00
|
|
|
sys::Program::ChangeStdoutToBinary();
|
|
|
|
|
2004-11-06 23:17:23 +00:00
|
|
|
// Create a vector of unsigned char for the bytecode output. We
|
|
|
|
// reserve 256KBytes of space in the vector so that we avoid doing
|
|
|
|
// lots of little allocations. 256KBytes is sufficient for a large
|
|
|
|
// proportion of the bytecode files we will encounter. Larger files
|
|
|
|
// will be automatically doubled in size as needed (std::vector
|
|
|
|
// behavior).
|
2004-07-25 18:07:36 +00:00
|
|
|
std::vector<unsigned char> Buffer;
|
2004-11-06 23:17:23 +00:00
|
|
|
Buffer.reserve(256 * 1024);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-11-06 23:17:23 +00:00
|
|
|
// The BytecodeWriter populates Buffer for us.
|
2004-07-25 18:07:36 +00:00
|
|
|
BytecodeWriter BCW(Buffer, M);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-11-06 23:17:23 +00:00
|
|
|
// Keep track of how much we've written
|
2002-07-26 18:40:14 +00:00
|
|
|
BytesWritten += Buffer.size();
|
|
|
|
|
2004-11-06 23:17:23 +00:00
|
|
|
// Determine start and end points of the Buffer
|
2004-11-07 18:17:38 +00:00
|
|
|
const unsigned char *FirstByte = &Buffer.front();
|
2004-11-06 23:17:23 +00:00
|
|
|
|
|
|
|
// If we're supposed to compress this mess ...
|
|
|
|
if (compress) {
|
|
|
|
|
|
|
|
// We signal compression by using an alternate magic number for the
|
2004-11-07 18:17:38 +00:00
|
|
|
// file. The compressed bytecode file's magic number is "llvc" instead
|
2005-04-21 21:48:46 +00:00
|
|
|
// of "llvm".
|
2004-11-07 18:17:38 +00:00
|
|
|
char compressed_magic[4];
|
|
|
|
compressed_magic[0] = 'l';
|
|
|
|
compressed_magic[1] = 'l';
|
|
|
|
compressed_magic[2] = 'v';
|
|
|
|
compressed_magic[3] = 'c';
|
2004-11-06 23:17:23 +00:00
|
|
|
|
2006-11-29 00:19:40 +00:00
|
|
|
Out.stream()->write(compressed_magic,4);
|
2004-11-06 23:17:23 +00:00
|
|
|
|
2004-11-14 22:01:41 +00:00
|
|
|
// Compress everything after the magic number (which we altered)
|
2006-11-02 20:25:50 +00:00
|
|
|
Compressor::compressToStream(
|
2004-11-06 23:17:23 +00:00
|
|
|
(char*)(FirstByte+4), // Skip the magic number
|
|
|
|
Buffer.size()-4, // Skip the magic number
|
2006-11-29 00:19:40 +00:00
|
|
|
*Out.stream() // Where to write compressed data
|
2004-11-06 23:17:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// We're not compressing, so just write the entire block.
|
2006-11-29 00:19:40 +00:00
|
|
|
Out.stream()->write((char*)FirstByte, Buffer.size());
|
2001-09-07 16:39:41 +00:00
|
|
|
}
|
2004-11-06 23:17:23 +00:00
|
|
|
|
|
|
|
// make sure it hits disk now
|
2006-11-29 00:19:40 +00:00
|
|
|
Out.stream()->flush();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|