* Use new style casts more
[oota-llvm.git] / lib / Bytecode / Writer / ConstantWriter.cpp
1 //===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=//
2 //
3 // This file implements the routines for encoding constants to a bytecode 
4 // stream.
5 //
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
8 // at least.  :)
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "WriterInternals.h"
13 #include "llvm/ConstPoolVals.h"
14 #include "llvm/SymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16
17 void BytecodeWriter::outputType(const Type *T) {
18   output_vbr((unsigned)T->getPrimitiveID(), Out);
19   
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
23   
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);
30
31     // Output the number of arguments to method (+1 if varargs):
32     output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
33
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);
40     }
41
42     // Terminate list with VoidTy if we are a varargs function...
43     if (MT->isVarArg())
44       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
45     break;
46   }
47
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;
54
55     output_vbr(AT->getNumElements(), Out);
56     break;
57   }
58
59   case Type::StructTyID: {
60     const StructType *ST = cast<const StructType>(T);
61
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);
68     }
69
70     // Terminate list with VoidTy
71     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
72     break;
73   }
74
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);
80     break;
81   }
82
83   //case Type::PackedTyID:
84   default:
85     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
86          << " Type '" << T->getDescription() << "'\n";
87     break;
88   }
89 }
90
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);
96     else
97       output_vbr((unsigned)0, Out);
98     break;
99
100   case Type::UByteTyID:   // Unsigned integer types...
101   case Type::UShortTyID:
102   case Type::UIntTyID:
103   case Type::ULongTyID:
104     output_vbr(cast<const ConstPoolUInt>(CPV)->getValue(), Out);
105     break;
106
107   case Type::SByteTyID:   // Signed integer types...
108   case Type::ShortTyID:
109   case Type::IntTyID:
110   case Type::LongTyID:
111     output_vbr(cast<const ConstPoolSInt>(CPV)->getValue(), Out);
112     break;
113
114   case Type::TypeTyID:     // Serialize type type
115     assert(0 && "Types should not be in the ConstPool!");
116     break;
117
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!!!
123
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);
128     }
129     break;
130   }
131
132   case Type::StructTyID: {
133     const ConstPoolStruct *CPS = cast<const ConstPoolStruct>(CPV);
134     const vector<Use> &Vals = CPS->getValues();
135
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);
140     }      
141     break;
142   }
143
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);
154     } else {
155       assert(0 && "Unknown ConstPoolPointer Subclass!");
156     }
157     break;
158   }
159
160   case Type::FloatTyID: {   // Floating point types...
161     float Tmp = (float)cast<ConstPoolFP>(CPV)->getValue();
162     output_data(&Tmp, &Tmp+1, Out);
163     break;
164   }
165   case Type::DoubleTyID: {
166     double Tmp = cast<ConstPoolFP>(CPV)->getValue();
167     output_data(&Tmp, &Tmp+1, Out);
168     break;
169   }
170
171   case Type::VoidTyID: 
172   case Type::LabelTyID:
173   default:
174     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
175          << " type '" << CPV->getType()->getName() << "'\n";
176     break;
177   }
178   return false;
179 }