blob: 835ef9850b43c0e453195993cee9e172ff0a7c17 [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"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000013#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#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 Lattner5855f0d2001-10-23 01:53:22 +000083 case Type::OpaqueTyID: {
84 // No need to emit anything, just the count of opaque types is enough.
85 break;
86 }
87
Chris Lattnere8fdde12001-09-07 16:39:41 +000088 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000089 default:
90 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
Chris Lattnere8fdde12001-09-07 16:39:41 +000091 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000092 break;
93 }
94}
95
Chris Lattnere9bb2df2001-12-03 22:26:30 +000096bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner00950542001-06-06 20:29:01 +000097 switch (CPV->getType()->getPrimitiveID()) {
98 case Type::BoolTyID: // Boolean Types
Chris Lattnere9bb2df2001-12-03 22:26:30 +000099 if (cast<const ConstantBool>(CPV)->getValue())
Chris Lattner00950542001-06-06 20:29:01 +0000100 output_vbr((unsigned)1, Out);
101 else
102 output_vbr((unsigned)0, Out);
103 break;
104
105 case Type::UByteTyID: // Unsigned integer types...
106 case Type::UShortTyID:
107 case Type::UIntTyID:
108 case Type::ULongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000109 output_vbr(cast<const ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000110 break;
111
112 case Type::SByteTyID: // Signed integer types...
113 case Type::ShortTyID:
114 case Type::IntTyID:
115 case Type::LongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 output_vbr(cast<const ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000117 break;
118
119 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000120 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000121 break;
122
123 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000124 const ConstantArray *CPA = cast<const ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000125 unsigned size = CPA->getValues().size();
126 if (!((const ArrayType *)CPA->getType())->isSized())
127 output_vbr(size, Out); // Not for sized arrays!!!
128
129 for (unsigned i = 0; i < size; i++) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000130 int Slot = Table.getValSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000131 assert(Slot != -1 && "Constant used but not available!!");
132 output_vbr((unsigned)Slot, Out);
133 }
134 break;
135 }
136
137 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000138 const ConstantStruct *CPS = cast<const ConstantStruct>(CPV);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000139 const vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 for (unsigned i = 0; i < Vals.size(); ++i) {
142 int Slot = Table.getValSlot(Vals[i]);
143 assert(Slot != -1 && "Constant used but not available!!");
144 output_vbr((unsigned)Slot, Out);
145 }
146 break;
147 }
148
Chris Lattner1a1cb112001-09-30 22:46:54 +0000149 case Type::PointerTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000150 const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
151 if (isa<ConstantPointerNull>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000152 output_vbr((unsigned)0, Out);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000153 } else if (const ConstantPointerRef *CPR =
154 dyn_cast<ConstantPointerRef>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000155 output_vbr((unsigned)1, Out);
156 int Slot = Table.getValSlot((Value*)CPR->getValue());
157 assert(Slot != -1 && "Global used but not available!!");
158 output_vbr((unsigned)Slot, Out);
159 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000160 assert(0 && "Unknown ConstantPointer Subclass!");
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000161 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000162 break;
163 }
164
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000165 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000166 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000167 output_data(&Tmp, &Tmp+1, Out);
168 break;
169 }
170 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000172 output_data(&Tmp, &Tmp+1, Out);
173 break;
174 }
Chris Lattner00950542001-06-06 20:29:01 +0000175
176 case Type::VoidTyID:
177 case Type::LabelTyID:
178 default:
179 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
180 << " type '" << CPV->getType()->getName() << "'\n";
181 break;
182 }
183 return false;
184}