Add support for extern varargs methods & varargs method calls
[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 the number of arguments to method (+1 if varargs):
32     output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
33
34     // Output all of the arguments...
35     MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
36     for (; I != MT->getParamTypes().end(); ++I) {
37       Slot = Table.getValSlot(*I);
38       assert(Slot != -1 && "Type used but not available!!");
39       output_vbr((unsigned)Slot, Out);
40     }
41
42     // Terminate list with VoidTy if we are a varargs function...
43     if (MT->isVarArg())
44       output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
45     break;
46   }
47
48   case Type::ArrayTyID: {
49     const ArrayType *AT = (const ArrayType*)T;
50     int Slot = Table.getValSlot(AT->getElementType());
51     assert(Slot != -1 && "Type used but not available!!");
52     output_vbr((unsigned)Slot, Out);
53     //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
54
55     output_vbr(AT->getNumElements(), Out);
56     break;
57   }
58
59   case Type::StructTyID: {
60     const StructType *ST = (const StructType*)T;
61
62     // Output all of the element types...
63     StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
64     for (; I != ST->getElementTypes().end(); ++I) {
65       int Slot = Table.getValSlot(*I);
66       assert(Slot != -1 && "Type used but not available!!");
67       output_vbr((unsigned)Slot, Out);
68     }
69
70     // Terminate list with VoidTy
71     output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
72     break;
73   }
74
75   case Type::PointerTyID: {
76     const PointerType *PT = (const PointerType*)T;
77     int Slot = Table.getValSlot(PT->getValueType());
78     assert(Slot != -1 && "Type used but not available!!");
79     output_vbr((unsigned)Slot, Out);
80     break;
81   }
82
83   case Type::ModuleTyID:
84   case Type::PackedTyID:
85   default:
86     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
87          << " Type '" << T->getName() << "'\n";
88     break;
89   }
90 }
91
92 bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
93   switch (CPV->getType()->getPrimitiveID()) {
94   case Type::BoolTyID:    // Boolean Types
95     if (((const ConstPoolBool*)CPV)->getValue())
96       output_vbr((unsigned)1, Out);
97     else
98       output_vbr((unsigned)0, Out);
99     break;
100
101   case Type::UByteTyID:   // Unsigned integer types...
102   case Type::UShortTyID:
103   case Type::UIntTyID:
104   case Type::ULongTyID:
105     output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out);
106     break;
107
108   case Type::SByteTyID:   // Signed integer types...
109   case Type::ShortTyID:
110   case Type::IntTyID:
111   case Type::LongTyID:
112     output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out);
113     break;
114
115   case Type::TypeTyID:     // Serialize type type
116     outputType(((const ConstPoolType*)CPV)->getValue());
117     break;
118
119   case Type::ArrayTyID: {
120     const ConstPoolArray *CPA = (const ConstPoolArray *)CPV;
121     unsigned size = CPA->getValues().size();
122     if (!((const ArrayType *)CPA->getType())->isSized())
123       output_vbr(size, Out);            // Not for sized arrays!!!
124
125     for (unsigned i = 0; i < size; i++) {
126       int Slot = Table.getValSlot(CPA->getValues()[i]);
127       assert(Slot != -1 && "Constant used but not available!!");
128       output_vbr((unsigned)Slot, Out);
129     }
130     break;
131   }
132
133   case Type::StructTyID: {
134     const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
135     const vector<Use> &Vals = CPS->getValues();
136
137     for (unsigned i = 0; i < Vals.size(); ++i) {
138       int Slot = Table.getValSlot(Vals[i]);
139       assert(Slot != -1 && "Constant used but not available!!");
140       output_vbr((unsigned)Slot, Out);
141     }      
142     break;
143   }
144
145   case Type::FloatTyID: {   // Floating point types...
146     float Tmp = (float)((const ConstPoolFP*)CPV)->getValue();
147     output_data(&Tmp, &Tmp+1, Out);
148     break;
149   }
150   case Type::DoubleTyID: {
151     double Tmp = ((const ConstPoolFP*)CPV)->getValue();
152     output_data(&Tmp, &Tmp+1, Out);
153     break;
154   }
155
156   case Type::VoidTyID: 
157   case Type::LabelTyID:
158   default:
159     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
160          << " type '" << CPV->getType()->getName() << "'\n";
161     break;
162   }
163   return false;
164 }