Added LLVM project notice to the top of every C++ source file.
[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 void BytecodeWriter::outputType(const Type *T) {
21   output_vbr((unsigned)T->getPrimitiveID(), Out);
22   
23   // That's all there is to handling primitive types...
24   if (T->isPrimitiveType())
25     return;     // We might do this if we alias a prim type: %x = type int
26   
27   switch (T->getPrimitiveID()) {   // Handle derived types now.
28   case Type::FunctionTyID: {
29     const FunctionType *MT = cast<FunctionType>(T);
30     int Slot = Table.getSlot(MT->getReturnType());
31     assert(Slot != -1 && "Type used but not available!!");
32     output_vbr((unsigned)Slot, Out);
33
34     // Output the number of arguments to method (+1 if varargs):
35     output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
36
37     // Output all of the arguments...
38     FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
39     for (; I != MT->getParamTypes().end(); ++I) {
40       Slot = Table.getSlot(*I);
41       assert(Slot != -1 && "Type used but not available!!");
42       output_vbr((unsigned)Slot, Out);
43     }
44
45     // Terminate list with VoidTy if we are a varargs function...
46     if (MT->isVarArg())
47       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
48     break;
49   }
50
51   case Type::ArrayTyID: {
52     const ArrayType *AT = cast<ArrayType>(T);
53     int Slot = Table.getSlot(AT->getElementType());
54     assert(Slot != -1 && "Type used but not available!!");
55     output_vbr((unsigned)Slot, Out);
56     //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
57
58     output_vbr(AT->getNumElements(), Out);
59     break;
60   }
61
62   case Type::StructTyID: {
63     const StructType *ST = cast<StructType>(T);
64
65     // Output all of the element types...
66     StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
67     for (; I != ST->getElementTypes().end(); ++I) {
68       int Slot = Table.getSlot(*I);
69       assert(Slot != -1 && "Type used but not available!!");
70       output_vbr((unsigned)Slot, Out);
71     }
72
73     // Terminate list with VoidTy
74     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
75     break;
76   }
77
78   case Type::PointerTyID: {
79     const PointerType *PT = cast<PointerType>(T);
80     int Slot = Table.getSlot(PT->getElementType());
81     assert(Slot != -1 && "Type used but not available!!");
82     output_vbr((unsigned)Slot, Out);
83     break;
84   }
85
86   case Type::OpaqueTyID: {
87     // No need to emit anything, just the count of opaque types is enough.
88     break;
89   }
90
91   //case Type::PackedTyID:
92   default:
93     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
94               << " Type '" << T->getDescription() << "'\n";
95     break;
96   }
97 }
98
99 bool BytecodeWriter::outputConstant(const Constant *CPV) {
100   assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
101          "Shouldn't output null constants!");
102
103   // We must check for a ConstantExpr before switching by type because
104   // a ConstantExpr can be of any type, and has no explicit value.
105   // 
106   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
107     // FIXME: Encoding of constant exprs could be much more compact!
108     assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
109     output_vbr(CE->getNumOperands(), Out);   // flags as an expr
110     output_vbr(CE->getOpcode(), Out);        // flags as an expr
111     
112     for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
113       int Slot = Table.getSlot(*OI);
114       assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
115       output_vbr((unsigned)Slot, Out);
116       Slot = Table.getSlot((*OI)->getType());
117       output_vbr((unsigned)Slot, Out);
118     }
119     return false;
120   } else {
121     output_vbr((unsigned)0, Out);       // flag as not a ConstantExpr
122   }
123   
124   switch (CPV->getType()->getPrimitiveID()) {
125   case Type::BoolTyID:    // Boolean Types
126     if (cast<ConstantBool>(CPV)->getValue())
127       output_vbr(1U, Out);
128     else
129       output_vbr(0U, Out);
130     break;
131
132   case Type::UByteTyID:   // Unsigned integer types...
133   case Type::UShortTyID:
134   case Type::UIntTyID:
135   case Type::ULongTyID:
136     output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
137     break;
138
139   case Type::SByteTyID:   // Signed integer types...
140   case Type::ShortTyID:
141   case Type::IntTyID:
142   case Type::LongTyID:
143     output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
144     break;
145
146   case Type::TypeTyID:     // Serialize type type
147     assert(0 && "Types should not be in the Constant!");
148     break;
149
150   case Type::ArrayTyID: {
151     const ConstantArray *CPA = cast<ConstantArray>(CPV);
152     unsigned size = CPA->getValues().size();
153     assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
154            && "ConstantArray out of whack!");
155     for (unsigned i = 0; i < size; i++) {
156       int Slot = Table.getSlot(CPA->getOperand(i));
157       assert(Slot != -1 && "Constant used but not available!!");
158       output_vbr((unsigned)Slot, Out);
159     }
160     break;
161   }
162
163   case Type::StructTyID: {
164     const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
165     const std::vector<Use> &Vals = CPS->getValues();
166
167     for (unsigned i = 0; i < Vals.size(); ++i) {
168       int Slot = Table.getSlot(Vals[i]);
169       assert(Slot != -1 && "Constant used but not available!!");
170       output_vbr((unsigned)Slot, Out);
171     }      
172     break;
173   }
174
175   case Type::PointerTyID: {
176     const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
177     assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
178     const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
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 }