Adjust to the changed StructType interface. In particular, getElementTypes() is...
[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 #include "Support/Statistic.h"
20 using 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
30   switch (T->getPrimitiveID()) {   // Handle derived types now.
31   case Type::FunctionTyID: {
32     const FunctionType *MT = cast<FunctionType>(T);
33     int Slot = Table.getSlot(MT->getReturnType());
34     assert(Slot != -1 && "Type used but not available!!");
35     output_vbr((unsigned)Slot, Out);
36
37     // Output the number of arguments to function (+1 if varargs):
38     output_vbr((unsigned)MT->getNumParams()+MT->isVarArg(), Out);
39
40     // Output all of the arguments...
41     FunctionType::param_iterator I = MT->param_begin();
42     for (; I != MT->param_end(); ++I) {
43       Slot = Table.getSlot(*I);
44       assert(Slot != -1 && "Type used but not available!!");
45       output_vbr((unsigned)Slot, Out);
46     }
47
48     // Terminate list with VoidTy if we are a varargs function...
49     if (MT->isVarArg())
50       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
51     break;
52   }
53
54   case Type::ArrayTyID: {
55     const ArrayType *AT = cast<ArrayType>(T);
56     int Slot = Table.getSlot(AT->getElementType());
57     assert(Slot != -1 && "Type used but not available!!");
58     output_vbr((unsigned)Slot, Out);
59     //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
60
61     output_vbr(AT->getNumElements(), Out);
62     break;
63   }
64
65   case Type::StructTyID: {
66     const StructType *ST = cast<StructType>(T);
67
68     // Output all of the element types...
69     for (StructType::element_iterator I = ST->element_begin(),
70            E = ST->element_end(); I != E; ++I) {
71       int Slot = Table.getSlot(*I);
72       assert(Slot != -1 && "Type used but not available!!");
73       output_vbr((unsigned)Slot, Out);
74     }
75
76     // Terminate list with VoidTy
77     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
78     break;
79   }
80
81   case Type::PointerTyID: {
82     const PointerType *PT = cast<PointerType>(T);
83     int Slot = Table.getSlot(PT->getElementType());
84     assert(Slot != -1 && "Type used but not available!!");
85     output_vbr((unsigned)Slot, Out);
86     break;
87   }
88
89   case Type::OpaqueTyID: {
90     // No need to emit anything, just the count of opaque types is enough.
91     break;
92   }
93
94   //case Type::PackedTyID:
95   default:
96     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
97               << " Type '" << T->getDescription() << "'\n";
98     break;
99   }
100 }
101
102 void BytecodeWriter::outputConstant(const Constant *CPV) {
103   assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
104          "Shouldn't output null constants!");
105
106   // We must check for a ConstantExpr before switching by type because
107   // a ConstantExpr can be of any type, and has no explicit value.
108   // 
109   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
110     // FIXME: Encoding of constant exprs could be much more compact!
111     assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
112     output_vbr(CE->getNumOperands(), Out);   // flags as an expr
113     output_vbr(CE->getOpcode(), Out);        // flags as an expr
114     
115     for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
116       int Slot = Table.getSlot(*OI);
117       assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
118       output_vbr((unsigned)Slot, Out);
119       Slot = Table.getSlot((*OI)->getType());
120       output_vbr((unsigned)Slot, Out);
121     }
122     return;
123   } else {
124     output_vbr(0U, Out);       // flag as not a ConstantExpr
125   }
126   
127   switch (CPV->getType()->getPrimitiveID()) {
128   case Type::BoolTyID:    // Boolean Types
129     if (cast<ConstantBool>(CPV)->getValue())
130       output_vbr(1U, Out);
131     else
132       output_vbr(0U, Out);
133     break;
134
135   case Type::UByteTyID:   // Unsigned integer types...
136   case Type::UShortTyID:
137   case Type::UIntTyID:
138   case Type::ULongTyID:
139     output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
140     break;
141
142   case Type::SByteTyID:   // Signed integer types...
143   case Type::ShortTyID:
144   case Type::IntTyID:
145   case Type::LongTyID:
146     output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
147     break;
148
149   case Type::TypeTyID:     // Serialize type type
150     assert(0 && "Types should not be in the Constant!");
151     break;
152
153   case Type::ArrayTyID: {
154     const ConstantArray *CPA = cast<ConstantArray>(CPV);
155     assert(!CPA->isString() && "Constant strings should be handled specially!");
156
157     for (unsigned i = 0; i != CPA->getNumOperands(); ++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     assert(0 && "No non-null, non-constant-expr constants allowed!");
179     abort();
180
181   case Type::FloatTyID: {   // Floating point types...
182     float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
183     output_data(&Tmp, &Tmp+1, Out);
184     break;
185   }
186   case Type::DoubleTyID: {
187     double Tmp = cast<ConstantFP>(CPV)->getValue();
188     output_data(&Tmp, &Tmp+1, Out);
189     break;
190   }
191
192   case Type::VoidTyID: 
193   case Type::LabelTyID:
194   default:
195     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
196               << " type '" << CPV->getType()->getName() << "'\n";
197     break;
198   }
199   return;
200 }
201
202 void BytecodeWriter::outputConstantStrings() {
203   SlotCalculator::string_iterator I = Table.string_begin();
204   SlotCalculator::string_iterator E = Table.string_end();
205   if (I == E) return;  // No strings to emit
206
207   // If we have != 0 strings to emit, output them now.  Strings are emitted into
208   // the 'void' type plane.
209   output_vbr(unsigned(E-I), Out);
210   output_vbr(Type::VoidTyID, Out);
211     
212   // Emit all of the strings.
213   for (I = Table.string_begin(); I != E; ++I) {
214     const ConstantArray *Str = *I;
215     int Slot = Table.getSlot(Str->getType());
216     assert(Slot != -1 && "Constant string of unknown type?");
217     output_vbr((unsigned)Slot, Out);
218     
219     // Now that we emitted the type (which indicates the size of the string),
220     // emit all of the characters.
221     std::string Val = Str->getAsString();
222     output_data(Val.c_str(), Val.c_str()+Val.size(), Out);
223   }
224 }