*** empty log message ***
[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/Constants.h"
14 #include "llvm/SymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include <iostream>
17 using std::cerr;
18
19 void BytecodeWriter::outputType(const Type *T) {
20   output_vbr((unsigned)T->getPrimitiveID(), Out);
21   
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
25   
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);
32
33     // Output the number of arguments to method (+1 if varargs):
34     output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
35
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);
42     }
43
44     // Terminate list with VoidTy if we are a varargs function...
45     if (MT->isVarArg())
46       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
47     break;
48   }
49
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;
56
57     output_vbr(AT->getNumElements(), Out);
58     break;
59   }
60
61   case Type::StructTyID: {
62     const StructType *ST = cast<const StructType>(T);
63
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);
70     }
71
72     // Terminate list with VoidTy
73     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
74     break;
75   }
76
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);
82     break;
83   }
84
85   case Type::OpaqueTyID: {
86     // No need to emit anything, just the count of opaque types is enough.
87     break;
88   }
89
90   //case Type::PackedTyID:
91   default:
92     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
93          << " Type '" << T->getDescription() << "'\n";
94     break;
95   }
96 }
97
98 bool BytecodeWriter::outputConstant(const Constant *CPV) {
99
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.
102   // 
103   if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CPV)) {
104     assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
105     output_vbr(CE->getNumOperands(), Out);   // flags as an expr
106     output_vbr(CE->getOpcode(), Out);        // flags as an expr
107     
108     for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
109       int Slot = Table.getValSlot(*OI);
110       assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
111       output_vbr((unsigned)Slot, Out);
112       Slot = Table.getValSlot((*OI)->getType());
113       output_vbr((unsigned)Slot, Out);
114     }
115     return false;
116   }
117   else
118     output_vbr((unsigned)0, Out);       // flag as not a ConstantExpr
119   
120   switch (CPV->getType()->getPrimitiveID()) {
121   case Type::BoolTyID:    // Boolean Types
122     if (cast<const ConstantBool>(CPV)->getValue())
123       output_vbr((unsigned)1, Out);
124     else
125       output_vbr((unsigned)0, Out);
126     break;
127
128   case Type::UByteTyID:   // Unsigned integer types...
129   case Type::UShortTyID:
130   case Type::UIntTyID:
131   case Type::ULongTyID:
132     output_vbr(cast<const ConstantUInt>(CPV)->getValue(), Out);
133     break;
134
135   case Type::SByteTyID:   // Signed integer types...
136   case Type::ShortTyID:
137   case Type::IntTyID:
138   case Type::LongTyID:
139     output_vbr(cast<const ConstantSInt>(CPV)->getValue(), Out);
140     break;
141
142   case Type::TypeTyID:     // Serialize type type
143     assert(0 && "Types should not be in the Constant!");
144     break;
145
146   case Type::ArrayTyID: {
147     const ConstantArray *CPA = cast<const ConstantArray>(CPV);
148     unsigned size = CPA->getValues().size();
149     assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
150            && "ConstantArray out of whack!");
151     for (unsigned i = 0; i < size; i++) {
152       int Slot = Table.getValSlot(CPA->getOperand(i));
153       assert(Slot != -1 && "Constant used but not available!!");
154       output_vbr((unsigned)Slot, Out);
155     }
156     break;
157   }
158
159   case Type::StructTyID: {
160     const ConstantStruct *CPS = cast<const ConstantStruct>(CPV);
161     const std::vector<Use> &Vals = CPS->getValues();
162
163     for (unsigned i = 0; i < Vals.size(); ++i) {
164       int Slot = Table.getValSlot(Vals[i]);
165       assert(Slot != -1 && "Constant used but not available!!");
166       output_vbr((unsigned)Slot, Out);
167     }      
168     break;
169   }
170
171   case Type::PointerTyID: {
172     const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
173     if (isa<ConstantPointerNull>(CPP)) {
174       output_vbr((unsigned)0, Out);
175     } else if (const ConstantPointerRef *CPR = 
176                         dyn_cast<ConstantPointerRef>(CPP)) {
177       output_vbr((unsigned)1, Out);
178       int Slot = Table.getValSlot((Value*)CPR->getValue());
179       assert(Slot != -1 && "Global used but not available!!");
180       output_vbr((unsigned)Slot, Out);
181     } else {
182       assert(0 && "Unknown ConstantPointer Subclass!");
183     }
184     break;
185   }
186
187   case Type::FloatTyID: {   // Floating point types...
188     float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
189     output_data(&Tmp, &Tmp+1, Out);
190     break;
191   }
192   case Type::DoubleTyID: {
193     double Tmp = cast<ConstantFP>(CPV)->getValue();
194     output_data(&Tmp, &Tmp+1, Out);
195     break;
196   }
197
198   case Type::VoidTyID: 
199   case Type::LabelTyID:
200   default:
201     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
202          << " type '" << CPV->getType()->getName() << "'\n";
203     break;
204   }
205   return false;
206 }