blob: f337ed17ffaf4358514183b03f81b7f63a790cfd [file] [log] [blame]
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001//===- ReadConst.cpp - Code to constants and constant pools ---------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements functionality to deserialize constants and entire
4// constant pools.
5//
6// Note that this library should be as fast as possible, reentrant, and
7// threadsafe!!
8//
Chris Lattnere9bb2df2001-12-03 22:26:30 +00009//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000010
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/Module.h"
Chris Lattnere4619182002-08-17 22:02:41 +000013#include "llvm/Constants.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000014#include <algorithm>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000015
Chris Lattner1d670cc2001-09-07 16:37:43 +000016const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
17 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000018 unsigned PrimType;
Chris Lattner74734132002-08-17 22:01:27 +000019 if (read_vbr(Buf, EndBuf, PrimType)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner1d670cc2001-09-07 16:37:43 +000021 const Type *Val = 0;
22 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
23 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000024
25 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000026 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000027 unsigned Typ;
Chris Lattner74734132002-08-17 22:01:27 +000028 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000029 const Type *RetType = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000030 if (RetType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattnere5a57ee2001-07-25 22:47:55 +000032 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000033 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000036 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000037 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000038 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000039 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000040 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000041 }
42
Chris Lattner05950c32001-10-13 06:47:01 +000043 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
44 if (isVarArg) Params.pop_back();
45
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000046 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000047 }
48 case Type::ArrayTyID: {
49 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000050 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000051 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000052 if (ElementType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattner90865152001-12-14 16:30:51 +000054 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000055 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000056
57 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000058 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000059 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000060 }
61 case Type::StructTyID: {
62 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000063 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner74734132002-08-17 22:01:27 +000065 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000066 while (Typ) { // List is terminated by void/0 typeid
67 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000068 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000069 Elements.push_back(Ty);
70
Chris Lattner74734132002-08-17 22:01:27 +000071 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000072 }
73
Chris Lattner7eadfa12001-10-24 06:21:22 +000074 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000075 }
76 case Type::PointerTyID: {
77 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000078 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner697954c2002-01-20 22:54:45 +000079 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
Chris Lattner00950542001-06-06 20:29:01 +000080 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000081 if (ElementType == 0) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000082 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000083 }
84
Chris Lattnerd48d6c72001-10-23 01:53:01 +000085 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000086 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000087 }
88
Chris Lattner00950542001-06-06 20:29:01 +000089 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000090 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000091 << ": Don't know how to deserialize"
92 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000093 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000094 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000095}
96
97// refineAbstractType - The callback method is invoked when one of the
98// elements of TypeValues becomes more concrete...
99//
100void BytecodeParser::refineAbstractType(const DerivedType *OldType,
101 const Type *NewType) {
Chris Lattnere244a252001-11-03 03:27:53 +0000102 if (OldType == NewType &&
103 OldType->isAbstract()) return; // Type is modified, but same
Chris Lattner05950c32001-10-13 06:47:01 +0000104
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
106 MethodTypeValues.end(), OldType);
107 if (I == MethodTypeValues.end()) {
108 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
109 assert(I != ModuleTypeValues.end() &&
110 "Can't refine a type I don't know about!");
111 }
112
Chris Lattnere244a252001-11-03 03:27:53 +0000113 if (OldType == NewType) {
114 assert(!OldType->isAbstract());
115 I->removeUserFromConcrete();
116 } else {
117 *I = NewType; // Update to point to new, more refined type.
118 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119}
120
121
122
123// parseTypeConstants - We have to use this wierd code to handle recursive
124// types. We know that recursive types will only reference the current slab of
125// values in the type plane, but they can forward reference types before they
126// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
127// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
128// this ugly problem, we pesimistically insert an opaque type for each type we
129// are about to read. This means that forward references will resolve to
130// something and when we reread the type later, we can replace the opaque type
131// with a new resolved concrete type.
132//
Chris Lattner92940ac2002-04-07 06:11:22 +0000133void debug_type_tables();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000134bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
135 TypeValuesListTy &Tab,
136 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000137 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138
139 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000140 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner1d670cc2001-09-07 16:37:43 +0000141 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
142
143 // Loop through reading all of the types. Forward types will make use of the
144 // opaque types just inserted.
145 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000146 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000147 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner74734132002-08-17 22:01:27 +0000148 if (NewTy == 0) return true;
Chris Lattner7eadfa12001-10-24 06:21:22 +0000149 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
150 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000151
152 // Don't insertValue the new type... instead we want to replace the opaque
153 // type with the new concrete value...
154 //
155
156 // Refine the abstract type to the new type. This causes all uses of the
157 // abstract type to use the newty. This also will cause the opaque type
158 // to be deleted...
159 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000160 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000161
162 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000163 // value table... or with a preexisting type that was already in the system
164 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000165 }
166
167 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000168 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000169 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000170 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000171 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000172 return false;
173}
174
Chris Lattner1d670cc2001-09-07 16:37:43 +0000175
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000176bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
177 const Type *Ty, Constant *&V) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000178
179 // We must check for a ConstantExpr before switching by type because
180 // a ConstantExpr can be of any type, and has no explicit value.
181 //
182 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Chris Lattner74734132002-08-17 22:01:27 +0000183 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000184 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000185 // FIXME: Encoding of constant exprs could be much more compact!
186 unsigned Opcode;
187 std::vector<Constant*> ArgVec;
188 ArgVec.reserve(isExprNumArgs);
Chris Lattner74734132002-08-17 22:01:27 +0000189 if (read_vbr(Buf, EndBuf, Opcode)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000190
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000191 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000192 for (unsigned i = 0; i != isExprNumArgs; ++i) {
193 unsigned ArgValSlot, ArgTypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000194 if (read_vbr(Buf, EndBuf, ArgValSlot)) return true;
195 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000196 const Type *ArgTy = getType(ArgTypeSlot);
Chris Lattner74734132002-08-17 22:01:27 +0000197 if (ArgTy == 0) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000198
Chris Lattnere8e46052002-07-30 18:54:22 +0000199 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: "
200 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000201
202 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000203 Constant *C = getConstantValue(ArgTy, ArgValSlot);
204 if (C == 0) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000205 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000206 }
207
208 // Construct a ConstantExpr of the appropriate kind
209 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000210 assert(Opcode == Instruction::Cast);
211 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000212 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
213 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
214 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000215 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000216 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000217 }
218 return false;
219 }
220
221 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000222 switch (Ty->getPrimitiveID()) {
223 case Type::BoolTyID: {
224 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000225 if (read_vbr(Buf, EndBuf, Val)) return true;
226 if (Val != 0 && Val != 1) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000227 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000228 break;
229 }
230
231 case Type::UByteTyID: // Unsigned integer types...
232 case Type::UShortTyID:
233 case Type::UIntTyID: {
234 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000235 if (read_vbr(Buf, EndBuf, Val)) return true;
236 if (!ConstantUInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000237 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000238 break;
239 }
240
241 case Type::ULongTyID: {
242 uint64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000243 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000244 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000245 break;
246 }
247
248 case Type::SByteTyID: // Unsigned integer types...
249 case Type::ShortTyID:
250 case Type::IntTyID: {
251 int Val;
Chris Lattner74734132002-08-17 22:01:27 +0000252 if (read_vbr(Buf, EndBuf, Val)) return true;
253 if (!ConstantSInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000254 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000255 break;
256 }
257
258 case Type::LongTyID: {
259 int64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000260 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000261 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000262 break;
263 }
264
Chris Lattnera1375302001-07-15 00:17:18 +0000265 case Type::FloatTyID: {
266 float F;
Chris Lattner74734132002-08-17 22:01:27 +0000267 if (input_data(Buf, EndBuf, &F, &F+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000268 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000269 break;
270 }
271
272 case Type::DoubleTyID: {
273 double Val;
Chris Lattner74734132002-08-17 22:01:27 +0000274 if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000275 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000276 break;
277 }
278
Chris Lattner00950542001-06-06 20:29:01 +0000279 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000280 assert(0 && "Type constants should be handled seperately!!!");
281 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000282
283 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000284 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000285 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000286
Chris Lattner697954c2002-01-20 22:54:45 +0000287 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000288 while (NumElements--) { // Read all of the elements of the constant.
289 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000290 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000291 Constant *C = getConstantValue(AT->getElementType(), Slot);
292 if (!C) return true;
293 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000294 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000296 break;
297 }
298
299 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000300 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000301 const StructType::ElementTypes &ET = ST->getElementTypes();
302
Chris Lattner697954c2002-01-20 22:54:45 +0000303 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000304 for (unsigned i = 0; i < ET.size(); ++i) {
305 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000306 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000307 Constant *C = getConstantValue(ET[i], Slot);
308 if (!C) return true;
309 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000310 }
311
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000313 break;
314 }
315
Chris Lattner1a1cb112001-09-30 22:46:54 +0000316 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000317 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000318 unsigned SubClass;
Chris Lattner74734132002-08-17 22:01:27 +0000319 if (read_vbr(Buf, EndBuf, SubClass)) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000320 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 case 0: // ConstantPointerNull value...
322 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000323 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000324
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000326 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000327 if (read_vbr(Buf, EndBuf, Slot)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000328 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000329
Chris Lattner74734132002-08-17 22:01:27 +0000330 // Check to see if we have already read this global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000331 Value *Val = getValue(PT, Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000332 GlobalValue *GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000333 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000334 if (!(GV = dyn_cast<GlobalValue>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000335 BCR_TRACE(5, "Value Found in ValueTable!\n");
336 } else { // Nope... find or create a forward ref. for it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000337 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PT, Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000338
339 if (I != GlobalRefs.end()) {
340 BCR_TRACE(5, "Previous forward ref found!\n");
341 GV = cast<GlobalValue>(I->second);
342 } else {
343 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
344
345 // Create a placeholder for the global variable reference...
346 GlobalVariable *GVar =
347 new GlobalVariable(PT->getElementType(), false, true);
348
349 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000350 GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
Chris Lattner74734132002-08-17 22:01:27 +0000351
352 // Must temporarily push this value into the module table...
353 TheModule->getGlobalList().push_back(GVar);
354 GV = GVar;
355 }
Chris Lattner05950c32001-10-13 06:47:01 +0000356 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000357 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000358 break;
359 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000360
Chris Lattner05950c32001-10-13 06:47:01 +0000361 default:
Chris Lattner90865152001-12-14 16:30:51 +0000362 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner74734132002-08-17 22:01:27 +0000363 return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000364 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000365 break;
366 }
367
Chris Lattner00950542001-06-06 20:29:01 +0000368 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000369 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000370 << ": Don't know how to deserialize constant value of type '"
371 << Ty->getName() << "'\n";
Chris Lattner74734132002-08-17 22:01:27 +0000372 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000373 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000374
Chris Lattner00950542001-06-06 20:29:01 +0000375 return false;
376}
377
378bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000379 ValueTable &Tab,
380 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000381 while (Buf < EndBuf) {
382 unsigned NumEntries, Typ;
383
384 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000385 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000386 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000387 if (Ty == 0) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000388 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000389
Chris Lattner1d670cc2001-09-07 16:37:43 +0000390 if (Typ == Type::TypeTyID) {
391 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
392 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000393 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000394 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000395 int Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000396 if (parseConstantValue(Buf, EndBuf, Ty, I)) return true;
397 assert(I && "parseConstantValue returned NULL!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000398 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner74734132002-08-17 22:01:27 +0000399 if ((Slot = insertValue(I, Tab)) < 0) return true;
400
401 // If we are reading a function constant table, make sure that we adjust
402 // the slot number to be the real global constant number.
403 //
404 if (&Tab != &ModuleValues)
405 Slot += ModuleValues[Typ].size();
406
407 ResolveReferencesToValue(I, (unsigned)Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000408 }
Chris Lattner00950542001-06-06 20:29:01 +0000409 }
410 }
411
Chris Lattner74734132002-08-17 22:01:27 +0000412 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000413 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000414}