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/ConstPoolVals.h"
14 #include "llvm/SymbolTable.h"
15 #include "llvm/DerivedTypes.h"
17 void BytecodeWriter::outputType(const Type *T) {
18 output_vbr((unsigned)T->getPrimitiveID(), Out);
20 // That's all there is to handling primitive types...
21 if (T->isPrimitiveType())
22 return; // We might do this if we alias a prim type: %x = type int
24 switch (T->getPrimitiveID()) { // Handle derived types now.
25 case Type::MethodTyID: {
26 const MethodType *MT = cast<const MethodType>(T);
27 int Slot = Table.getValSlot(MT->getReturnType());
28 assert(Slot != -1 && "Type used but not available!!");
29 output_vbr((unsigned)Slot, Out);
31 // Output the number of arguments to method (+1 if varargs):
32 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
34 // Output all of the arguments...
35 MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
36 for (; I != MT->getParamTypes().end(); ++I) {
37 Slot = Table.getValSlot(*I);
38 assert(Slot != -1 && "Type used but not available!!");
39 output_vbr((unsigned)Slot, Out);
42 // Terminate list with VoidTy if we are a varargs function...
44 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
48 case Type::ArrayTyID: {
49 const ArrayType *AT = cast<const ArrayType>(T);
50 int Slot = Table.getValSlot(AT->getElementType());
51 assert(Slot != -1 && "Type used but not available!!");
52 output_vbr((unsigned)Slot, Out);
53 //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
55 output_vbr(AT->getNumElements(), Out);
59 case Type::StructTyID: {
60 const StructType *ST = cast<const StructType>(T);
62 // Output all of the element types...
63 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
64 for (; I != ST->getElementTypes().end(); ++I) {
65 int Slot = Table.getValSlot(*I);
66 assert(Slot != -1 && "Type used but not available!!");
67 output_vbr((unsigned)Slot, Out);
70 // Terminate list with VoidTy
71 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
75 case Type::PointerTyID: {
76 const PointerType *PT = cast<const PointerType>(T);
77 int Slot = Table.getValSlot(PT->getValueType());
78 assert(Slot != -1 && "Type used but not available!!");
79 output_vbr((unsigned)Slot, Out);
83 //case Type::PackedTyID:
85 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
86 << " Type '" << T->getDescription() << "'\n";
91 bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
92 switch (CPV->getType()->getPrimitiveID()) {
93 case Type::BoolTyID: // Boolean Types
94 if (cast<const ConstPoolBool>(CPV)->getValue())
95 output_vbr((unsigned)1, Out);
97 output_vbr((unsigned)0, Out);
100 case Type::UByteTyID: // Unsigned integer types...
101 case Type::UShortTyID:
103 case Type::ULongTyID:
104 output_vbr(cast<const ConstPoolUInt>(CPV)->getValue(), Out);
107 case Type::SByteTyID: // Signed integer types...
108 case Type::ShortTyID:
111 output_vbr(cast<const ConstPoolSInt>(CPV)->getValue(), Out);
114 case Type::TypeTyID: // Serialize type type
115 assert(0 && "Types should not be in the ConstPool!");
118 case Type::ArrayTyID: {
119 const ConstPoolArray *CPA = cast<const ConstPoolArray>(CPV);
120 unsigned size = CPA->getValues().size();
121 if (!((const ArrayType *)CPA->getType())->isSized())
122 output_vbr(size, Out); // Not for sized arrays!!!
124 for (unsigned i = 0; i < size; i++) {
125 int Slot = Table.getValSlot(CPA->getOperand(i));
126 assert(Slot != -1 && "Constant used but not available!!");
127 output_vbr((unsigned)Slot, Out);
132 case Type::StructTyID: {
133 const ConstPoolStruct *CPS = cast<const ConstPoolStruct>(CPV);
134 const vector<Use> &Vals = CPS->getValues();
136 for (unsigned i = 0; i < Vals.size(); ++i) {
137 int Slot = Table.getValSlot(Vals[i]);
138 assert(Slot != -1 && "Constant used but not available!!");
139 output_vbr((unsigned)Slot, Out);
144 case Type::PointerTyID: {
145 const ConstPoolPointer *CPP = cast<const ConstPoolPointer>(CPV);
146 if (isa<ConstPoolPointerNull>(CPP)) {
147 output_vbr((unsigned)0, Out);
148 } else if (const ConstPoolPointerReference *CPR =
149 dyn_cast<ConstPoolPointerReference>(CPP)) {
150 output_vbr((unsigned)1, Out);
151 int Slot = Table.getValSlot((Value*)CPR->getValue());
152 assert(Slot != -1 && "Global used but not available!!");
153 output_vbr((unsigned)Slot, Out);
155 assert(0 && "Unknown ConstPoolPointer Subclass!");
160 case Type::FloatTyID: { // Floating point types...
161 float Tmp = (float)cast<ConstPoolFP>(CPV)->getValue();
162 output_data(&Tmp, &Tmp+1, Out);
165 case Type::DoubleTyID: {
166 double Tmp = cast<ConstPoolFP>(CPV)->getValue();
167 output_data(&Tmp, &Tmp+1, Out);
172 case Type::LabelTyID:
174 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
175 << " type '" << CPV->getType()->getName() << "'\n";