blob: d0c58f1ae6d147b1a61ed27c4b67528d6d1287fe [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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
17void 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: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000026 const MethodType *MT = cast<const MethodType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000027 int Slot = Table.getValSlot(MT->getReturnType());
28 assert(Slot != -1 && "Type used but not available!!");
29 output_vbr((unsigned)Slot, Out);
30
Chris Lattnere5a57ee2001-07-25 22:47:55 +000031 // Output the number of arguments to method (+1 if varargs):
32 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
33
Chris Lattner00950542001-06-06 20:29:01 +000034 // Output all of the arguments...
35 MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000036 for (; I != MT->getParamTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000037 Slot = Table.getValSlot(*I);
38 assert(Slot != -1 && "Type used but not available!!");
39 output_vbr((unsigned)Slot, Out);
40 }
41
Chris Lattnere5a57ee2001-07-25 22:47:55 +000042 // Terminate list with VoidTy if we are a varargs function...
43 if (MT->isVarArg())
44 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000045 break;
46 }
47
48 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000049 const ArrayType *AT = cast<const ArrayType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000050 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: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000060 const StructType *ST = cast<const StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000061
62 // Output all of the element types...
63 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000064 for (; I != ST->getElementTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000065 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: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000076 const PointerType *PT = cast<const PointerType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000077 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
Chris Lattnere8fdde12001-09-07 16:39:41 +000083 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000084 default:
85 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
Chris Lattnere8fdde12001-09-07 16:39:41 +000086 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000087 break;
88 }
89}
90
91bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
92 switch (CPV->getType()->getPrimitiveID()) {
93 case Type::BoolTyID: // Boolean Types
Chris Lattnerb00c5822001-10-02 03:41:24 +000094 if (cast<const ConstPoolBool>(CPV)->getValue())
Chris Lattner00950542001-06-06 20:29:01 +000095 output_vbr((unsigned)1, Out);
96 else
97 output_vbr((unsigned)0, Out);
98 break;
99
100 case Type::UByteTyID: // Unsigned integer types...
101 case Type::UShortTyID:
102 case Type::UIntTyID:
103 case Type::ULongTyID:
104 output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out);
105 break;
106
107 case Type::SByteTyID: // Signed integer types...
108 case Type::ShortTyID:
109 case Type::IntTyID:
110 case Type::LongTyID:
111 output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out);
112 break;
113
114 case Type::TypeTyID: // Serialize type type
Chris Lattnere8fdde12001-09-07 16:39:41 +0000115 assert(0 && "Types should not be in the ConstPool!");
Chris Lattner00950542001-06-06 20:29:01 +0000116 break;
117
118 case Type::ArrayTyID: {
119 const ConstPoolArray *CPA = (const ConstPoolArray *)CPV;
120 unsigned size = CPA->getValues().size();
121 if (!((const ArrayType *)CPA->getType())->isSized())
122 output_vbr(size, Out); // Not for sized arrays!!!
123
124 for (unsigned i = 0; i < size; i++) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000125 int Slot = Table.getValSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000126 assert(Slot != -1 && "Constant used but not available!!");
127 output_vbr((unsigned)Slot, Out);
128 }
129 break;
130 }
131
132 case Type::StructTyID: {
133 const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000134 const vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000135
136 for (unsigned i = 0; i < Vals.size(); ++i) {
137 int Slot = Table.getValSlot(Vals[i]);
138 assert(Slot != -1 && "Constant used but not available!!");
139 output_vbr((unsigned)Slot, Out);
140 }
141 break;
142 }
143
Chris Lattner1a1cb112001-09-30 22:46:54 +0000144 case Type::PointerTyID: {
145 output_vbr((unsigned)0, Out);
146 break;
147 }
148
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000149 case Type::FloatTyID: { // Floating point types...
150 float Tmp = (float)((const ConstPoolFP*)CPV)->getValue();
151 output_data(&Tmp, &Tmp+1, Out);
152 break;
153 }
154 case Type::DoubleTyID: {
155 double Tmp = ((const ConstPoolFP*)CPV)->getValue();
156 output_data(&Tmp, &Tmp+1, Out);
157 break;
158 }
Chris Lattner00950542001-06-06 20:29:01 +0000159
160 case Type::VoidTyID:
161 case Type::LabelTyID:
162 default:
163 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
164 << " type '" << CPV->getType()->getName() << "'\n";
165 break;
166 }
167 return false;
168}