Changed the fundemental architecture of Operands for Instructions. Now
[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/ConstPoolVals.h"
14 #include "llvm/SymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16
17 void BytecodeWriter::outputType(const Type *T) {
18   output_vbr((unsigned)T->getPrimitiveID(), Out);
19   
20   // That's all there is to handling primitive types...
21   if (T->isPrimitiveType())
22     return;     // We might do this if we alias a prim type: %x = type int
23   
24   switch (T->getPrimitiveID()) {   // Handle derived types now.
25   case Type::MethodTyID: {
26     const MethodType *MT = (const MethodType*)T;
27     int Slot = Table.getValSlot(MT->getReturnType());
28     assert(Slot != -1 && "Type used but not available!!");
29     output_vbr((unsigned)Slot, Out);
30
31     // Output all of the arguments...
32     MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
33     for (; I != MT->getParamTypes().end(); ++I) {
34       Slot = Table.getValSlot(*I);
35       assert(Slot != -1 && "Type used but not available!!");
36       output_vbr((unsigned)Slot, Out);
37     }
38
39     // Terminate list with VoidTy
40     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
41     break;
42   }
43
44   case Type::ArrayTyID: {
45     const ArrayType *AT = (const ArrayType*)T;
46     int Slot = Table.getValSlot(AT->getElementType());
47     assert(Slot != -1 && "Type used but not available!!");
48     output_vbr((unsigned)Slot, Out);
49     //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 = (const 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.getValSlot(*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 = (const PointerType*)T;
73     int Slot = Table.getValSlot(PT->getValueType());
74     assert(Slot != -1 && "Type used but not available!!");
75     output_vbr((unsigned)Slot, Out);
76     break;
77   }
78
79   case Type::ModuleTyID:
80   case Type::PackedTyID:
81   default:
82     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
83          << " Type '" << T->getName() << "'\n";
84     break;
85   }
86 }
87
88 bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
89   switch (CPV->getType()->getPrimitiveID()) {
90   case Type::BoolTyID:    // Boolean Types
91     if (((const ConstPoolBool*)CPV)->getValue())
92       output_vbr((unsigned)1, Out);
93     else
94       output_vbr((unsigned)0, Out);
95     break;
96
97   case Type::UByteTyID:   // Unsigned integer types...
98   case Type::UShortTyID:
99   case Type::UIntTyID:
100   case Type::ULongTyID:
101     output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out);
102     break;
103
104   case Type::SByteTyID:   // Signed integer types...
105   case Type::ShortTyID:
106   case Type::IntTyID:
107   case Type::LongTyID:
108     output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out);
109     break;
110
111   case Type::TypeTyID:     // Serialize type type
112     outputType(((const ConstPoolType*)CPV)->getValue());
113     break;
114
115   case Type::ArrayTyID: {
116     const ConstPoolArray *CPA = (const ConstPoolArray *)CPV;
117     unsigned size = CPA->getValues().size();
118     if (!((const ArrayType *)CPA->getType())->isSized())
119       output_vbr(size, Out);            // Not for sized arrays!!!
120
121     for (unsigned i = 0; i < size; i++) {
122       int Slot = Table.getValSlot(CPA->getValues()[i]);
123       assert(Slot != -1 && "Constant used but not available!!");
124       output_vbr((unsigned)Slot, Out);
125     }
126     break;
127   }
128
129   case Type::StructTyID: {
130     const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
131     const vector<Use> &Vals = CPS->getValues();
132
133     for (unsigned i = 0; i < Vals.size(); ++i) {
134       int Slot = Table.getValSlot(Vals[i]);
135       assert(Slot != -1 && "Constant used but not available!!");
136       output_vbr((unsigned)Slot, Out);
137     }      
138     break;
139   }
140
141   case Type::FloatTyID:    // Floating point types...
142   case Type::DoubleTyID:
143     // TODO: Floating point type serialization
144
145
146   case Type::VoidTyID: 
147   case Type::LabelTyID:
148   default:
149     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
150          << " type '" << CPV->getType()->getName() << "'\n";
151     break;
152   }
153   return false;
154 }