1 //===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=//
3 // This file implements the routines for encoding constants to a bytecode
6 // Note that the performance of this library is not terribly important, because
7 // it shouldn't be used by JIT type applications... so it is not a huge focus
10 //===----------------------------------------------------------------------===//
12 #include "WriterInternals.h"
13 #include "llvm/Constants.h"
14 #include "llvm/SymbolTable.h"
15 #include "llvm/DerivedTypes.h"
19 void BytecodeWriter::outputType(const Type *T) {
20 output_vbr((unsigned)T->getPrimitiveID(), Out);
22 // That's all there is to handling primitive types...
23 if (T->isPrimitiveType())
24 return; // We might do this if we alias a prim type: %x = type int
26 switch (T->getPrimitiveID()) { // Handle derived types now.
27 case Type::FunctionTyID: {
28 const FunctionType *MT = cast<const FunctionType>(T);
29 int Slot = Table.getValSlot(MT->getReturnType());
30 assert(Slot != -1 && "Type used but not available!!");
31 output_vbr((unsigned)Slot, Out);
33 // Output the number of arguments to method (+1 if varargs):
34 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
36 // Output all of the arguments...
37 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
38 for (; I != MT->getParamTypes().end(); ++I) {
39 Slot = Table.getValSlot(*I);
40 assert(Slot != -1 && "Type used but not available!!");
41 output_vbr((unsigned)Slot, Out);
44 // Terminate list with VoidTy if we are a varargs function...
46 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
50 case Type::ArrayTyID: {
51 const ArrayType *AT = cast<const ArrayType>(T);
52 int Slot = Table.getValSlot(AT->getElementType());
53 assert(Slot != -1 && "Type used but not available!!");
54 output_vbr((unsigned)Slot, Out);
55 //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
57 output_vbr(AT->getNumElements(), Out);
61 case Type::StructTyID: {
62 const StructType *ST = cast<const StructType>(T);
64 // Output all of the element types...
65 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
66 for (; I != ST->getElementTypes().end(); ++I) {
67 int Slot = Table.getValSlot(*I);
68 assert(Slot != -1 && "Type used but not available!!");
69 output_vbr((unsigned)Slot, Out);
72 // Terminate list with VoidTy
73 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
77 case Type::PointerTyID: {
78 const PointerType *PT = cast<const PointerType>(T);
79 int Slot = Table.getValSlot(PT->getElementType());
80 assert(Slot != -1 && "Type used but not available!!");
81 output_vbr((unsigned)Slot, Out);
85 case Type::OpaqueTyID: {
86 // No need to emit anything, just the count of opaque types is enough.
90 //case Type::PackedTyID:
92 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
93 << " Type '" << T->getDescription() << "'\n";
98 bool BytecodeWriter::outputConstant(const Constant *CPV) {
100 // We must check for a ConstantExpr before switching by type because
101 // a ConstantExpr can be of any type, and has no explicit value.
103 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
104 // FIXME: Encoding of constant exprs could be much more compact!
105 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
106 output_vbr(CE->getNumOperands(), Out); // flags as an expr
107 output_vbr(CE->getOpcode(), Out); // flags as an expr
109 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
110 int Slot = Table.getValSlot(*OI);
111 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
112 output_vbr((unsigned)Slot, Out);
113 Slot = Table.getValSlot((*OI)->getType());
114 output_vbr((unsigned)Slot, Out);
118 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
121 switch (CPV->getType()->getPrimitiveID()) {
122 case Type::BoolTyID: // Boolean Types
123 if (cast<const ConstantBool>(CPV)->getValue())
124 output_vbr((unsigned)1, Out);
126 output_vbr((unsigned)0, Out);
129 case Type::UByteTyID: // Unsigned integer types...
130 case Type::UShortTyID:
132 case Type::ULongTyID:
133 output_vbr(cast<const ConstantUInt>(CPV)->getValue(), Out);
136 case Type::SByteTyID: // Signed integer types...
137 case Type::ShortTyID:
140 output_vbr(cast<const ConstantSInt>(CPV)->getValue(), Out);
143 case Type::TypeTyID: // Serialize type type
144 assert(0 && "Types should not be in the Constant!");
147 case Type::ArrayTyID: {
148 const ConstantArray *CPA = cast<const ConstantArray>(CPV);
149 unsigned size = CPA->getValues().size();
150 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
151 && "ConstantArray out of whack!");
152 for (unsigned i = 0; i < size; i++) {
153 int Slot = Table.getValSlot(CPA->getOperand(i));
154 assert(Slot != -1 && "Constant used but not available!!");
155 output_vbr((unsigned)Slot, Out);
160 case Type::StructTyID: {
161 const ConstantStruct *CPS = cast<const ConstantStruct>(CPV);
162 const std::vector<Use> &Vals = CPS->getValues();
164 for (unsigned i = 0; i < Vals.size(); ++i) {
165 int Slot = Table.getValSlot(Vals[i]);
166 assert(Slot != -1 && "Constant used but not available!!");
167 output_vbr((unsigned)Slot, Out);
172 case Type::PointerTyID: {
173 const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
174 if (isa<ConstantPointerNull>(CPP)) {
175 output_vbr((unsigned)0, Out);
176 } else if (const ConstantPointerRef *CPR =
177 dyn_cast<ConstantPointerRef>(CPP)) {
178 output_vbr((unsigned)1, Out);
179 int Slot = Table.getValSlot((Value*)CPR->getValue());
180 assert(Slot != -1 && "Global used but not available!!");
181 output_vbr((unsigned)Slot, Out);
183 assert(0 && "Unknown ConstantPointer Subclass!");
188 case Type::FloatTyID: { // Floating point types...
189 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
190 output_data(&Tmp, &Tmp+1, Out);
193 case Type::DoubleTyID: {
194 double Tmp = cast<ConstantFP>(CPV)->getValue();
195 output_data(&Tmp, &Tmp+1, Out);
200 case Type::LabelTyID:
202 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
203 << " type '" << CPV->getType()->getName() << "'\n";