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