2003-10-13 03:32:08 +00:00
|
|
|
//===-- ConstantWriter.cpp - Functions for writing constants --------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file implements the routines for encoding constants to a bytecode
|
|
|
|
// stream.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WriterInternals.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-01-14 16:54:21 +00:00
|
|
|
#include "Support/Statistic.h"
|
2004-01-10 18:49:43 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
void BytecodeWriter::outputType(const Type *T) {
|
2004-06-17 18:19:28 +00:00
|
|
|
output_vbr((unsigned)T->getTypeID(), Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// That's all there is to handling primitive types...
|
2004-01-14 16:54:21 +00:00
|
|
|
if (T->isPrimitiveType()) {
|
2001-06-06 20:29:01 +00:00
|
|
|
return; // We might do this if we alias a prim type: %x = type int
|
2004-01-14 16:54:21 +00:00
|
|
|
}
|
|
|
|
|
2004-06-17 18:19:28 +00:00
|
|
|
switch (T->getTypeID()) { // Handle derived types now.
|
2002-03-29 03:51:11 +00:00
|
|
|
case Type::FunctionTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const FunctionType *MT = cast<FunctionType>(T);
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(MT->getReturnType());
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
|
2004-02-09 04:14:01 +00:00
|
|
|
// Output the number of arguments to function (+1 if varargs):
|
|
|
|
output_vbr((unsigned)MT->getNumParams()+MT->isVarArg(), Out);
|
2001-07-25 22:47:55 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// Output all of the arguments...
|
2004-02-09 04:14:01 +00:00
|
|
|
FunctionType::param_iterator I = MT->param_begin();
|
|
|
|
for (; I != MT->param_end(); ++I) {
|
2003-10-17 02:02:40 +00:00
|
|
|
Slot = Table.getSlot(*I);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
|
|
|
|
2001-07-25 22:47:55 +00:00
|
|
|
// Terminate list with VoidTy if we are a varargs function...
|
|
|
|
if (MT->isVarArg())
|
2004-06-17 18:19:28 +00:00
|
|
|
output_vbr((unsigned)Type::VoidTyID, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const ArrayType *AT = cast<ArrayType>(T);
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(AT->getElementType());
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
2003-03-19 20:56:46 +00:00
|
|
|
//std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
output_vbr(AT->getNumElements(), Out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const StructType *ST = cast<StructType>(T);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Output all of the element types...
|
2004-02-09 04:37:31 +00:00
|
|
|
for (StructType::element_iterator I = ST->element_begin(),
|
|
|
|
E = ST->element_end(); I != E; ++I) {
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(*I);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate list with VoidTy
|
2004-06-17 18:19:28 +00:00
|
|
|
output_vbr((unsigned)Type::VoidTyID, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PointerTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const PointerType *PT = cast<PointerType>(T);
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(PT->getElementType());
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-10-23 01:53:22 +00:00
|
|
|
case Type::OpaqueTyID: {
|
|
|
|
// No need to emit anything, just the count of opaque types is enough.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:39:41 +00:00
|
|
|
//case Type::PackedTyID:
|
2001-06-06 20:29:01 +00:00
|
|
|
default:
|
2003-03-19 20:56:46 +00:00
|
|
|
std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
|
|
|
<< " Type '" << T->getDescription() << "'\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:36:54 +00:00
|
|
|
void BytecodeWriter::outputConstant(const Constant *CPV) {
|
2003-03-19 20:56:46 +00:00
|
|
|
assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
|
|
|
|
"Shouldn't output null constants!");
|
2002-07-14 23:08:30 +00:00
|
|
|
|
|
|
|
// We must check for a ConstantExpr before switching by type because
|
|
|
|
// a ConstantExpr can be of any type, and has no explicit value.
|
|
|
|
//
|
2002-07-30 18:54:25 +00:00
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
|
|
|
|
// FIXME: Encoding of constant exprs could be much more compact!
|
2002-07-14 23:08:30 +00:00
|
|
|
assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
|
|
|
|
output_vbr(CE->getNumOperands(), Out); // flags as an expr
|
|
|
|
output_vbr(CE->getOpcode(), Out); // flags as an expr
|
|
|
|
|
|
|
|
for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(*OI);
|
2002-07-14 23:08:30 +00:00
|
|
|
assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
2003-10-17 02:02:40 +00:00
|
|
|
Slot = Table.getSlot((*OI)->getType());
|
2002-07-14 23:08:30 +00:00
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
2004-01-14 23:36:54 +00:00
|
|
|
return;
|
2002-07-30 18:54:25 +00:00
|
|
|
} else {
|
2004-01-14 16:54:21 +00:00
|
|
|
output_vbr(0U, Out); // flag as not a ConstantExpr
|
2002-07-30 18:54:25 +00:00
|
|
|
}
|
2002-07-14 23:08:30 +00:00
|
|
|
|
2004-06-17 18:19:28 +00:00
|
|
|
switch (CPV->getType()->getTypeID()) {
|
2001-06-06 20:29:01 +00:00
|
|
|
case Type::BoolTyID: // Boolean Types
|
2003-07-23 14:54:33 +00:00
|
|
|
if (cast<ConstantBool>(CPV)->getValue())
|
2003-03-19 20:56:46 +00:00
|
|
|
output_vbr(1U, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
else
|
2003-03-19 20:56:46 +00:00
|
|
|
output_vbr(0U, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::UByteTyID: // Unsigned integer types...
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID:
|
|
|
|
case Type::ULongTyID:
|
2003-07-23 14:54:33 +00:00
|
|
|
output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::SByteTyID: // Signed integer types...
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID:
|
|
|
|
case Type::LongTyID:
|
2003-07-23 14:54:33 +00:00
|
|
|
output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::TypeTyID: // Serialize type type
|
2001-12-03 22:26:30 +00:00
|
|
|
assert(0 && "Types should not be in the Constant!");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const ConstantArray *CPA = cast<ConstantArray>(CPV);
|
2004-01-14 23:36:54 +00:00
|
|
|
assert(!CPA->isString() && "Constant strings should be handled specially!");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != CPA->getNumOperands(); ++i) {
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(CPA->getOperand(i));
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Constant used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
2003-07-23 14:54:33 +00:00
|
|
|
const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Use> &Vals = CPS->getValues();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < Vals.size(); ++i) {
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(Vals[i]);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Constant used but not available!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
2004-01-14 16:54:21 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-15 18:46:56 +00:00
|
|
|
case Type::PointerTyID:
|
|
|
|
assert(0 && "No non-null, non-constant-expr constants allowed!");
|
|
|
|
abort();
|
2001-09-30 22:46:54 +00:00
|
|
|
|
2001-07-15 00:17:23 +00:00
|
|
|
case Type::FloatTyID: { // Floating point types...
|
2001-12-03 22:26:30 +00:00
|
|
|
float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
|
2001-07-15 00:17:23 +00:00
|
|
|
output_data(&Tmp, &Tmp+1, Out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::DoubleTyID: {
|
2001-12-03 22:26:30 +00:00
|
|
|
double Tmp = cast<ConstantFP>(CPV)->getValue();
|
2001-07-15 00:17:23 +00:00
|
|
|
output_data(&Tmp, &Tmp+1, Out);
|
|
|
|
break;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
case Type::VoidTyID:
|
|
|
|
case Type::LabelTyID:
|
|
|
|
default:
|
2003-03-19 20:56:46 +00:00
|
|
|
std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
|
|
|
<< " type '" << CPV->getType()->getName() << "'\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-01-14 23:36:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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), Out);
|
|
|
|
output_vbr(Type::VoidTyID, Out);
|
|
|
|
|
|
|
|
// 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_vbr((unsigned)Slot, Out);
|
|
|
|
|
|
|
|
// 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(), Out);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|