Remove usage of ConstantPointer
[oota-llvm.git] / lib / Bytecode / Writer / ConstantWriter.cpp
1 //===-- ConstantWriter.cpp - Functions for writing constants --------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the routines for encoding constants to a bytecode 
11 // stream.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "WriterInternals.h"
16 #include "llvm/Constants.h"
17 #include "llvm/SymbolTable.h"
18 #include "llvm/DerivedTypes.h"
19
20 namespace llvm {
21
22 void BytecodeWriter::outputType(const Type *T) {
23   output_vbr((unsigned)T->getPrimitiveID(), Out);
24   
25   // That's all there is to handling primitive types...
26   if (T->isPrimitiveType())
27     return;     // We might do this if we alias a prim type: %x = type int
28   
29   switch (T->getPrimitiveID()) {   // Handle derived types now.
30   case Type::FunctionTyID: {
31     const FunctionType *MT = cast<FunctionType>(T);
32     int Slot = Table.getSlot(MT->getReturnType());
33     assert(Slot != -1 && "Type used but not available!!");
34     output_vbr((unsigned)Slot, Out);
35
36     // Output the number of arguments to method (+1 if varargs):
37     output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
38
39     // Output all of the arguments...
40     FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
41     for (; I != MT->getParamTypes().end(); ++I) {
42       Slot = Table.getSlot(*I);
43       assert(Slot != -1 && "Type used but not available!!");
44       output_vbr((unsigned)Slot, Out);
45     }
46
47     // Terminate list with VoidTy if we are a varargs function...
48     if (MT->isVarArg())
49       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
50     break;
51   }
52
53   case Type::ArrayTyID: {
54     const ArrayType *AT = cast<ArrayType>(T);
55     int Slot = Table.getSlot(AT->getElementType());
56     assert(Slot != -1 && "Type used but not available!!");
57     output_vbr((unsigned)Slot, Out);
58     //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
59
60     output_vbr(AT->getNumElements(), Out);
61     break;
62   }
63
64   case Type::StructTyID: {
65     const StructType *ST = cast<StructType>(T);
66
67     // Output all of the element types...
68     StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
69     for (; I != ST->getElementTypes().end(); ++I) {
70       int Slot = Table.getSlot(*I);
71       assert(Slot != -1 && "Type used but not available!!");
72       output_vbr((unsigned)Slot, Out);
73     }
74
75     // Terminate list with VoidTy
76     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
77     break;
78   }
79
80   case Type::PointerTyID: {
81     const PointerType *PT = cast<PointerType>(T);
82     int Slot = Table.getSlot(PT->getElementType());
83     assert(Slot != -1 && "Type used but not available!!");
84     output_vbr((unsigned)Slot, Out);
85     break;
86   }
87
88   case Type::OpaqueTyID: {
89     // No need to emit anything, just the count of opaque types is enough.
90     break;
91   }
92
93   //case Type::PackedTyID:
94   default:
95     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
96               << " Type '" << T->getDescription() << "'\n";
97     break;
98   }
99 }
100
101 bool BytecodeWriter::outputConstant(const Constant *CPV) {
102   assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
103          "Shouldn't output null constants!");
104
105   // We must check for a ConstantExpr before switching by type because
106   // a ConstantExpr can be of any type, and has no explicit value.
107   // 
108   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
109     // FIXME: Encoding of constant exprs could be much more compact!
110     assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
111     output_vbr(CE->getNumOperands(), Out);   // flags as an expr
112     output_vbr(CE->getOpcode(), Out);        // flags as an expr
113     
114     for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
115       int Slot = Table.getSlot(*OI);
116       assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
117       output_vbr((unsigned)Slot, Out);
118       Slot = Table.getSlot((*OI)->getType());
119       output_vbr((unsigned)Slot, Out);
120     }
121     return false;
122   } else {
123     output_vbr((unsigned)0, Out);       // flag as not a ConstantExpr
124   }
125   
126   switch (CPV->getType()->getPrimitiveID()) {
127   case Type::BoolTyID:    // Boolean Types
128     if (cast<ConstantBool>(CPV)->getValue())
129       output_vbr(1U, Out);
130     else
131       output_vbr(0U, Out);
132     break;
133
134   case Type::UByteTyID:   // Unsigned integer types...
135   case Type::UShortTyID:
136   case Type::UIntTyID:
137   case Type::ULongTyID:
138     output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
139     break;
140
141   case Type::SByteTyID:   // Signed integer types...
142   case Type::ShortTyID:
143   case Type::IntTyID:
144   case Type::LongTyID:
145     output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
146     break;
147
148   case Type::TypeTyID:     // Serialize type type
149     assert(0 && "Types should not be in the Constant!");
150     break;
151
152   case Type::ArrayTyID: {
153     const ConstantArray *CPA = cast<ConstantArray>(CPV);
154     unsigned size = CPA->getValues().size();
155     assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
156            && "ConstantArray out of whack!");
157     for (unsigned i = 0; i < size; i++) {
158       int Slot = Table.getSlot(CPA->getOperand(i));
159       assert(Slot != -1 && "Constant used but not available!!");
160       output_vbr((unsigned)Slot, Out);
161     }
162     break;
163   }
164
165   case Type::StructTyID: {
166     const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
167     const std::vector<Use> &Vals = CPS->getValues();
168
169     for (unsigned i = 0; i < Vals.size(); ++i) {
170       int Slot = Table.getSlot(Vals[i]);
171       assert(Slot != -1 && "Constant used but not available!!");
172       output_vbr((unsigned)Slot, Out);
173     }      
174     break;
175   }
176
177   case Type::PointerTyID: {
178     const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPV);
179     int Slot = Table.getSlot((Value*)CPR->getValue());
180     assert(Slot != -1 && "Global used but not available!!");
181     output_vbr((unsigned)Slot, Out);
182     break;
183   }
184
185   case Type::FloatTyID: {   // Floating point types...
186     float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
187     output_data(&Tmp, &Tmp+1, Out);
188     break;
189   }
190   case Type::DoubleTyID: {
191     double Tmp = cast<ConstantFP>(CPV)->getValue();
192     output_data(&Tmp, &Tmp+1, Out);
193     break;
194   }
195
196   case Type::VoidTyID: 
197   case Type::LabelTyID:
198   default:
199     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
200               << " type '" << CPV->getType()->getName() << "'\n";
201     break;
202   }
203   return false;
204 }
205
206 } // End llvm namespace