1 //===-- ConstantWriter.cpp - Functions for writing constants --------------===//
3 // This file implements the routines for encoding constants to a bytecode
6 //===----------------------------------------------------------------------===//
8 #include "WriterInternals.h"
9 #include "llvm/Constants.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/DerivedTypes.h"
13 void BytecodeWriter::outputType(const Type *T) {
14 output_vbr((unsigned)T->getPrimitiveID(), Out);
16 // That's all there is to handling primitive types...
17 if (T->isPrimitiveType())
18 return; // We might do this if we alias a prim type: %x = type int
20 switch (T->getPrimitiveID()) { // Handle derived types now.
21 case Type::FunctionTyID: {
22 const FunctionType *MT = cast<FunctionType>(T);
23 int Slot = Table.getSlot(MT->getReturnType());
24 assert(Slot != -1 && "Type used but not available!!");
25 output_vbr((unsigned)Slot, Out);
27 // Output the number of arguments to method (+1 if varargs):
28 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
30 // Output all of the arguments...
31 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
32 for (; I != MT->getParamTypes().end(); ++I) {
33 Slot = Table.getSlot(*I);
34 assert(Slot != -1 && "Type used but not available!!");
35 output_vbr((unsigned)Slot, Out);
38 // Terminate list with VoidTy if we are a varargs function...
40 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
44 case Type::ArrayTyID: {
45 const ArrayType *AT = cast<ArrayType>(T);
46 int Slot = Table.getSlot(AT->getElementType());
47 assert(Slot != -1 && "Type used but not available!!");
48 output_vbr((unsigned)Slot, Out);
49 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
51 output_vbr(AT->getNumElements(), Out);
55 case Type::StructTyID: {
56 const StructType *ST = cast<StructType>(T);
58 // Output all of the element types...
59 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
60 for (; I != ST->getElementTypes().end(); ++I) {
61 int Slot = Table.getSlot(*I);
62 assert(Slot != -1 && "Type used but not available!!");
63 output_vbr((unsigned)Slot, Out);
66 // Terminate list with VoidTy
67 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
71 case Type::PointerTyID: {
72 const PointerType *PT = cast<PointerType>(T);
73 int Slot = Table.getSlot(PT->getElementType());
74 assert(Slot != -1 && "Type used but not available!!");
75 output_vbr((unsigned)Slot, Out);
79 case Type::OpaqueTyID: {
80 // No need to emit anything, just the count of opaque types is enough.
84 //case Type::PackedTyID:
86 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
87 << " Type '" << T->getDescription() << "'\n";
92 bool BytecodeWriter::outputConstant(const Constant *CPV) {
93 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
94 "Shouldn't output null constants!");
96 // We must check for a ConstantExpr before switching by type because
97 // a ConstantExpr can be of any type, and has no explicit value.
99 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
100 // FIXME: Encoding of constant exprs could be much more compact!
101 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
102 output_vbr(CE->getNumOperands(), Out); // flags as an expr
103 output_vbr(CE->getOpcode(), Out); // flags as an expr
105 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
106 int Slot = Table.getSlot(*OI);
107 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
108 output_vbr((unsigned)Slot, Out);
109 Slot = Table.getSlot((*OI)->getType());
110 output_vbr((unsigned)Slot, Out);
114 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
117 switch (CPV->getType()->getPrimitiveID()) {
118 case Type::BoolTyID: // Boolean Types
119 if (cast<ConstantBool>(CPV)->getValue())
125 case Type::UByteTyID: // Unsigned integer types...
126 case Type::UShortTyID:
128 case Type::ULongTyID:
129 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
132 case Type::SByteTyID: // Signed integer types...
133 case Type::ShortTyID:
136 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
139 case Type::TypeTyID: // Serialize type type
140 assert(0 && "Types should not be in the Constant!");
143 case Type::ArrayTyID: {
144 const ConstantArray *CPA = cast<ConstantArray>(CPV);
145 unsigned size = CPA->getValues().size();
146 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
147 && "ConstantArray out of whack!");
148 for (unsigned i = 0; i < size; i++) {
149 int Slot = Table.getSlot(CPA->getOperand(i));
150 assert(Slot != -1 && "Constant used but not available!!");
151 output_vbr((unsigned)Slot, Out);
156 case Type::StructTyID: {
157 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
158 const std::vector<Use> &Vals = CPS->getValues();
160 for (unsigned i = 0; i < Vals.size(); ++i) {
161 int Slot = Table.getSlot(Vals[i]);
162 assert(Slot != -1 && "Constant used but not available!!");
163 output_vbr((unsigned)Slot, Out);
168 case Type::PointerTyID: {
169 const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
170 assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
171 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
172 int Slot = Table.getSlot((Value*)CPR->getValue());
173 assert(Slot != -1 && "Global used but not available!!");
174 output_vbr((unsigned)Slot, Out);
178 case Type::FloatTyID: { // Floating point types...
179 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
180 output_data(&Tmp, &Tmp+1, Out);
183 case Type::DoubleTyID: {
184 double Tmp = cast<ConstantFP>(CPV)->getValue();
185 output_data(&Tmp, &Tmp+1, Out);
190 case Type::LabelTyID:
192 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
193 << " type '" << CPV->getType()->getName() << "'\n";