blob: 2067f222110a9bb6d5bdf1fee99c008b88feb905 [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Bytecode/Reader.h
11//
12// Note that this library should be as fast as possible, reentrant, and
13// threadsafe!!
14//
Chris Lattner00950542001-06-06 20:29:01 +000015// TODO: Allow passing in an option to ignore the symbol table
16//
Chris Lattnerd6b65252001-10-24 01:15:12 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner7061dc52001-12-03 18:02:31 +000019#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Bytecode/Reader.h"
21#include "llvm/Bytecode/Format.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000022#include "llvm/Module.h"
23#include "Support/StringExtras.h"
Chris Lattner29b789b2003-11-19 17:27:18 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner9e460f22003-10-04 20:00:03 +000026unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
27 if (Ty->isPrimitiveType())
28 return Ty->getPrimitiveID();
29
Chris Lattner89e02532004-01-18 21:08:15 +000030 // Scan the compaction table for the type if needed.
31 if (CompactionTable.size() > Type::TypeTyID) {
32 std::vector<Value*> &Plane = CompactionTable[Type::TypeTyID];
33 if (!Plane.empty()) {
34 std::vector<Value*>::iterator I = find(Plane.begin(), Plane.end(),
35 const_cast<Type*>(Ty));
36 if (I == Plane.end())
37 throw std::string("Couldn't find type specified in compaction table!");
38 return Type::FirstDerivedTyID + (&*I - &Plane[0]);
39 }
40 }
41
Chris Lattner9e460f22003-10-04 20:00:03 +000042 // Check the function level types first...
43 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
44 FunctionTypeValues.end(), Ty);
45 if (I != FunctionTypeValues.end())
Chris Lattner89e02532004-01-18 21:08:15 +000046 return Type::FirstDerivedTyID + ModuleTypeValues.size() +
Chris Lattner9e460f22003-10-04 20:00:03 +000047 (&*I - &FunctionTypeValues[0]);
48
49 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
50 if (I == ModuleTypeValues.end())
51 throw std::string("Didn't find type in ModuleTypeValues.");
Chris Lattner89e02532004-01-18 21:08:15 +000052 return Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000053}
54
55const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner697954c2002-01-20 22:54:45 +000056 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner36392bc2003-10-08 21:18:57 +000057
Chris Lattner89e02532004-01-18 21:08:15 +000058 if (ID < Type::FirstDerivedTyID)
Chris Lattner927b1852003-10-09 20:22:47 +000059 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
60 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +000061
62 // Otherwise, derived types need offset...
Chris Lattner89e02532004-01-18 21:08:15 +000063 ID -= Type::FirstDerivedTyID;
64
65 if (CompactionTable.size() > Type::TypeTyID &&
66 !CompactionTable[Type::TypeTyID].empty()) {
67 if (ID >= CompactionTable[Type::TypeTyID].size())
68 throw std::string("Type ID out of range for compaction table!");
69 return cast<Type>(CompactionTable[Type::TypeTyID][ID]);
70 }
Chris Lattner36392bc2003-10-08 21:18:57 +000071
72 // Is it a module-level type?
73 if (ID < ModuleTypeValues.size())
74 return ModuleTypeValues[ID].get();
75
76 // Nope, is it a function-level type?
77 ID -= ModuleTypeValues.size();
78 if (ID < FunctionTypeValues.size())
79 return FunctionTypeValues[ID].get();
80
Chris Lattner927b1852003-10-09 20:22:47 +000081 throw std::string("Illegal type reference!");
Chris Lattner00950542001-06-06 20:29:01 +000082}
83
Chris Lattner80b97342004-01-17 23:25:43 +000084static inline bool hasImplicitNull(unsigned TyID, bool EncodesPrimitiveZeros) {
85 if (!EncodesPrimitiveZeros)
86 return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
87 TyID != Type::VoidTyID;
88 return TyID >= Type::FirstDerivedTyID;
89}
90
Chris Lattnerf0d92732003-10-13 14:34:59 +000091unsigned BytecodeParser::insertValue(Value *Val, unsigned type,
92 ValueTable &ValueTab) {
Chris Lattner80b97342004-01-17 23:25:43 +000093 assert((!isa<Constant>(Val) || !cast<Constant>(Val)->isNullValue()) ||
94 !hasImplicitNull(type, hasExplicitPrimitiveZeros) &&
Chris Lattner52e20b02003-03-19 20:54:26 +000095 "Cannot read null values from bytecode!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000096 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner29b789b2003-11-19 17:27:18 +000097
Chris Lattner89e02532004-01-18 21:08:15 +000098 if (ValueTab.size() <= type)
Chris Lattner52e20b02003-03-19 20:54:26 +000099 ValueTab.resize(type+1);
Chris Lattner00950542001-06-06 20:29:01 +0000100
Chris Lattner29b789b2003-11-19 17:27:18 +0000101 if (!ValueTab[type]) ValueTab[type] = new ValueList();
102
Chris Lattner00950542001-06-06 20:29:01 +0000103 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +0000104 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +0000105 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +0000106
Chris Lattner89e02532004-01-18 21:08:15 +0000107 bool HasOffset = hasImplicitNull(type, hasExplicitPrimitiveZeros);
Chris Lattner52e20b02003-03-19 20:54:26 +0000108 return ValueTab[type]->size()-1 + HasOffset;
109}
110
Chris Lattner36392bc2003-10-08 21:18:57 +0000111Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
112 assert(type != Type::TypeTyID && "getValue() cannot get types!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000113 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000114 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000115
Chris Lattner89e02532004-01-18 21:08:15 +0000116 // If there is a compaction table active, it defines the low-level numbers.
117 // If not, the module values define the low-level numbers.
118 if (CompactionTable.size() > type && !CompactionTable[type].empty()) {
119 if (Num < CompactionTable[type].size())
120 return CompactionTable[type][Num];
121 Num -= CompactionTable[type].size();
122 } else {
Chris Lattner00950542001-06-06 20:29:01 +0000123
Chris Lattner89e02532004-01-18 21:08:15 +0000124 if (hasImplicitNull(type, hasExplicitPrimitiveZeros)) {
125 if (Num == 0)
126 return Constant::getNullValue(getType(type));
127 --Num;
128 }
129
130 if (type < ModuleValues.size() && ModuleValues[type]) {
131 if (Num < ModuleValues[type]->size())
132 return ModuleValues[type]->getOperand(Num);
133 Num -= ModuleValues[type]->size();
134 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000135 }
136
Chris Lattner29b789b2003-11-19 17:27:18 +0000137 if (Values.size() > type && Values[type] && Num < Values[type]->size())
Chris Lattner52e20b02003-03-19 20:54:26 +0000138 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000139
Chris Lattner74734132002-08-17 22:01:27 +0000140 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000142 std::pair<unsigned,unsigned> KeyValue(type, oNum);
143 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
144 ForwardReferences.lower_bound(KeyValue);
145 if (I != ForwardReferences.end() && I->first == KeyValue)
146 return I->second; // We have already created this placeholder
147
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000148 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000149 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000150 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000151}
152
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000153/// getBasicBlock - Get a particular numbered basic block, which might be a
154/// forward reference. This works together with ParseBasicBlock to handle these
155/// forward references in a clean manner.
156///
157BasicBlock *BytecodeParser::getBasicBlock(unsigned ID) {
158 // Make sure there is room in the table...
159 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
160
161 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
162 // has already created this block, or if the forward reference has already
163 // been created.
164 if (ParsedBasicBlocks[ID])
165 return ParsedBasicBlocks[ID];
166
167 // Otherwise, the basic block has not yet been created. Do so and add it to
168 // the ParsedBasicBlocks list.
169 return ParsedBasicBlocks[ID] = new BasicBlock();
170}
171
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000172/// getConstantValue - Just like getValue, except that it returns a null pointer
173/// only on error. It always returns a constant (meaning that if the value is
174/// defined, but is not a constant, that is an error). If the specified
175/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
176/// after the real value is parsed, the placeholder is eliminated.
177///
Chris Lattner1c3673b2003-11-19 06:01:12 +0000178Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
179 if (Value *V = getValue(TypeSlot, Slot, false))
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000180 if (Constant *C = dyn_cast<Constant>(V))
181 return C; // If we already have the value parsed, just return it
Chris Lattner93361992004-01-15 18:45:25 +0000182 else if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
183 // ConstantPointerRef's are an abomination, but at least they don't have
184 // to infest bytecode files.
185 return ConstantPointerRef::get(GV);
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000186 else
187 throw std::string("Reference of a value is expected to be a constant!");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000188
Chris Lattner1c3673b2003-11-19 06:01:12 +0000189 const Type *Ty = getType(TypeSlot);
Chris Lattner52e20b02003-03-19 20:54:26 +0000190 std::pair<const Type*, unsigned> Key(Ty, Slot);
Chris Lattner29b789b2003-11-19 17:27:18 +0000191 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
Chris Lattner52e20b02003-03-19 20:54:26 +0000192
Chris Lattner29b789b2003-11-19 17:27:18 +0000193 if (I != ConstantFwdRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000194 BCR_TRACE(5, "Previous forward ref found!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000195 return I->second;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000196 } else {
197 // Create a placeholder for the constant reference and
198 // keep track of the fact that we have a forward ref to recycle it
199 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
200 Constant *C = new ConstPHolder(Ty, Slot);
201
202 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner29b789b2003-11-19 17:27:18 +0000203 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000204 return C;
205 }
206}
207
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000208/// ParseBasicBlock - In LLVM 1.0 bytecode files, we used to output one
209/// basicblock at a time. This method reads in one of the basicblock packets.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000210BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
211 const unsigned char *EndBuf,
212 unsigned BlockNo) {
213 BasicBlock *BB;
214 if (ParsedBasicBlocks.size() == BlockNo)
215 ParsedBasicBlocks.push_back(BB = new BasicBlock());
216 else if (ParsedBasicBlocks[BlockNo] == 0)
217 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
218 else
219 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000220
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000221 std::vector<unsigned> Args;
222 while (Buf < EndBuf)
223 ParseInstruction(Buf, EndBuf, Args, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000224
Misha Brukman12c29d12003-09-22 23:38:23 +0000225 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000226}
227
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000228
229/// ParseInstructionList - Parse all of the BasicBlock's & Instruction's in the
230/// body of a function. In post 1.0 bytecode files, we no longer emit basic
231/// block individually, in order to avoid per-basic-block overhead.
232unsigned BytecodeParser::ParseInstructionList(Function *F,
233 const unsigned char *&Buf,
234 const unsigned char *EndBuf) {
235 unsigned BlockNo = 0;
236 std::vector<unsigned> Args;
237
238 while (Buf < EndBuf) {
239 BasicBlock *BB;
240 if (ParsedBasicBlocks.size() == BlockNo)
241 ParsedBasicBlocks.push_back(BB = new BasicBlock());
242 else if (ParsedBasicBlocks[BlockNo] == 0)
243 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
244 else
245 BB = ParsedBasicBlocks[BlockNo];
246 ++BlockNo;
247 F->getBasicBlockList().push_back(BB);
248
249 // Read instructions into this basic block until we get to a terminator
250 while (Buf < EndBuf && !BB->getTerminator())
251 ParseInstruction(Buf, EndBuf, Args, BB);
252
253 if (!BB->getTerminator())
254 throw std::string("Non-terminated basic block found!");
255 }
256
257 return BlockNo;
258}
259
Misha Brukman12c29d12003-09-22 23:38:23 +0000260void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000261 const unsigned char *EndBuf,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000262 SymbolTable *ST,
263 Function *CurrentFunction) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000264 // Allow efficient basic block lookup by number.
265 std::vector<BasicBlock*> BBMap;
266 if (CurrentFunction)
267 for (Function::iterator I = CurrentFunction->begin(),
268 E = CurrentFunction->end(); I != E; ++I)
269 BBMap.push_back(I);
270
Chris Lattner00950542001-06-06 20:29:01 +0000271 while (Buf < EndBuf) {
272 // Symtab block header: [num entries][type id number]
Chris Lattner7969dc22004-01-15 06:13:09 +0000273 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
274 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000275 const Type *Ty = getType(Typ);
Chris Lattner927b1852003-10-09 20:22:47 +0000276 BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000277 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000278
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000279 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000280 // Symtab entry: [def slot #][name]
Chris Lattner7969dc22004-01-15 06:13:09 +0000281 unsigned slot = read_vbr_uint(Buf, EndBuf);
282 std::string Name = read_str(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000283
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000284 Value *V = 0;
Chris Lattner36392bc2003-10-08 21:18:57 +0000285 if (Typ == Type::TypeTyID)
286 V = (Value*)getType(slot);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000287 else if (Typ == Type::LabelTyID) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000288 if (slot < BBMap.size())
289 V = BBMap[slot];
290 } else {
Chris Lattner36392bc2003-10-08 21:18:57 +0000291 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +0000292 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000293 if (V == 0) throw std::string("Failed value look-up.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000294 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000295 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000296
Chris Lattner52e20b02003-03-19 20:54:26 +0000297 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
299 }
300
Misha Brukman12c29d12003-09-22 23:38:23 +0000301 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000302}
303
Chris Lattner29b789b2003-11-19 17:27:18 +0000304void BytecodeParser::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
305 ConstantRefsType::iterator I =
306 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
307 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000308
Chris Lattner74734132002-08-17 22:01:27 +0000309 BCR_TRACE(3, "Mutating forward refs!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000310 Value *PH = I->second; // Get the placeholder...
311 PH->replaceAllUsesWith(NewV);
312 delete PH; // Delete the old placeholder
313 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000314}
315
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000316void BytecodeParser::ParseFunction(const unsigned char *&Buf,
317 const unsigned char *EndBuf) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000318 if (FunctionSignatureList.empty())
319 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000320
Chris Lattner29b789b2003-11-19 17:27:18 +0000321 Function *F = FunctionSignatureList.back();
Misha Brukman12c29d12003-09-22 23:38:23 +0000322 FunctionSignatureList.pop_back();
323
324 // Save the information for future reading of the function
Chris Lattner29b789b2003-11-19 17:27:18 +0000325 LazyFunctionLoadMap[F] = LazyFunctionInfo(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000326 // Pretend we've `parsed' this function
327 Buf = EndBuf;
328}
329
330void BytecodeParser::materializeFunction(Function* F) {
331 // Find {start, end} pointers and slot in the map. If not there, we're done.
Chris Lattner29b789b2003-11-19 17:27:18 +0000332 std::map<Function*, LazyFunctionInfo>::iterator Fi =
Misha Brukman12c29d12003-09-22 23:38:23 +0000333 LazyFunctionLoadMap.find(F);
334 if (Fi == LazyFunctionLoadMap.end()) return;
Chris Lattner29b789b2003-11-19 17:27:18 +0000335
336 const unsigned char *Buf = Fi->second.Buf;
337 const unsigned char *EndBuf = Fi->second.EndBuf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000338 LazyFunctionLoadMap.erase(Fi);
Chris Lattner00950542001-06-06 20:29:01 +0000339
Chris Lattnere3869c82003-04-16 21:16:05 +0000340 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
341
Chris Lattner7969dc22004-01-15 06:13:09 +0000342 unsigned LinkageType = read_vbr_uint(Buf, EndBuf);
Chris Lattnerc08912f2004-01-14 16:44:44 +0000343 if ((!hasExtendedLinkageSpecs && LinkageType > 3) ||
344 ( hasExtendedLinkageSpecs && LinkageType > 4))
345 throw std::string("Invalid linkage type for Function.");
346 switch (LinkageType) {
347 case 0: Linkage = GlobalValue::ExternalLinkage; break;
348 case 1: Linkage = GlobalValue::WeakLinkage; break;
349 case 2: Linkage = GlobalValue::AppendingLinkage; break;
350 case 3: Linkage = GlobalValue::InternalLinkage; break;
351 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattnere3869c82003-04-16 21:16:05 +0000352 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000353
Chris Lattnere3869c82003-04-16 21:16:05 +0000354 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000355
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000356 // Keep track of how many basic blocks we have read in...
357 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +0000358 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000359
Chris Lattner00950542001-06-06 20:29:01 +0000360 while (Buf < EndBuf) {
361 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000362 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000363 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000364
365 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +0000366 case BytecodeFormat::ConstantPool:
Chris Lattner89e02532004-01-18 21:08:15 +0000367 if (!InsertedArguments) {
368 // Insert arguments into the value table before we parse the first basic
369 // block in the function, but after we potentially read in the
370 // compaction table.
371 const FunctionType::ParamTypes &Params =
372 F->getFunctionType()->getParamTypes();
373 Function::aiterator AI = F->abegin();
374 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
375 It != Params.end(); ++It, ++AI)
376 insertValue(AI, getTypeSlot(AI->getType()), Values);
377 InsertedArguments = true;
378 }
379
Chris Lattner1d670cc2001-09-07 16:37:43 +0000380 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000381 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000382 break;
383
Chris Lattner89e02532004-01-18 21:08:15 +0000384 case BytecodeFormat::CompactionTable:
385 BCR_TRACE(2, "BLOCK BytecodeFormat::CompactionTable: {\n");
386 ParseCompactionTable(Buf, Buf+Size);
387 break;
388
Chris Lattner00950542001-06-06 20:29:01 +0000389 case BytecodeFormat::BasicBlock: {
Chris Lattner89e02532004-01-18 21:08:15 +0000390 if (!InsertedArguments) {
391 // Insert arguments into the value table before we parse the first basic
392 // block in the function, but after we potentially read in the
393 // compaction table.
394 const FunctionType::ParamTypes &Params =
395 F->getFunctionType()->getParamTypes();
396 Function::aiterator AI = F->abegin();
397 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
398 It != Params.end(); ++It, ++AI)
399 insertValue(AI, getTypeSlot(AI->getType()), Values);
400 InsertedArguments = true;
401 }
402
Chris Lattner1d670cc2001-09-07 16:37:43 +0000403 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000404 BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
405 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000406 break;
407 }
408
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000409 case BytecodeFormat::InstructionList: {
Chris Lattner89e02532004-01-18 21:08:15 +0000410 // Insert arguments into the value table before we parse the instruction
411 // list for the function, but after we potentially read in the compaction
412 // table.
413 if (!InsertedArguments) {
414 const FunctionType::ParamTypes &Params =
415 F->getFunctionType()->getParamTypes();
416 Function::aiterator AI = F->abegin();
417 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
418 It != Params.end(); ++It, ++AI)
419 insertValue(AI, getTypeSlot(AI->getType()), Values);
420 InsertedArguments = true;
421 }
422
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000423 BCR_TRACE(2, "BLOCK BytecodeFormat::InstructionList: {\n");
424 if (BlockNum) throw std::string("Already parsed basic blocks!");
425 BlockNum = ParseInstructionList(F, Buf, Buf+Size);
426 break;
427 }
428
Chris Lattner29b789b2003-11-19 17:27:18 +0000429 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000430 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000431 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
Chris Lattner00950542001-06-06 20:29:01 +0000432 break;
433
434 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000435 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000436 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000437 if (OldBuf > Buf)
438 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000439 break;
440 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000441 BCR_TRACE(2, "} end block\n");
442
Misha Brukman12c29d12003-09-22 23:38:23 +0000443 // Malformed bc file if read past end of block.
Chris Lattner7969dc22004-01-15 06:13:09 +0000444 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000445 }
446
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000447 // Make sure there were no references to non-existant basic blocks.
448 if (BlockNum != ParsedBasicBlocks.size())
449 throw std::string("Illegal basic block operand reference");
450 ParsedBasicBlocks.clear();
451
Chris Lattner97330cf2003-10-09 23:10:14 +0000452 // Resolve forward references. Replace any uses of a forward reference value
453 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000454
Chris Lattner97330cf2003-10-09 23:10:14 +0000455 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
456 // number of operands. PHI nodes often have forward references, and can also
457 // often have a very large number of operands.
Chris Lattner89e02532004-01-18 21:08:15 +0000458 //
459 // FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code
460 // should be simplified back to using it!
461 //
Chris Lattner97330cf2003-10-09 23:10:14 +0000462 std::map<Value*, Value*> ForwardRefMapping;
463 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
464 I = ForwardReferences.begin(), E = ForwardReferences.end();
465 I != E; ++I)
466 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
467 false);
468
469 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
470 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
471 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
472 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
473 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
474 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
475 }
476
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000477 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +0000478 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
479 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000480 Value *PlaceHolder = I->second;
481 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +0000482
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000483 // Now that all the uses are gone, delete the placeholder...
484 // If we couldn't find a def (error case), then leak a little
485 // memory, because otherwise we can't remove all uses!
486 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +0000487 }
Chris Lattner00950542001-06-06 20:29:01 +0000488
Misha Brukman12c29d12003-09-22 23:38:23 +0000489 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000490 FunctionTypeValues.clear();
Chris Lattner89e02532004-01-18 21:08:15 +0000491 CompactionTable.clear();
Chris Lattner52e20b02003-03-19 20:54:26 +0000492 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000493}
494
Chris Lattner89e02532004-01-18 21:08:15 +0000495void BytecodeParser::ParseCompactionTable(const unsigned char *&Buf,
496 const unsigned char *End) {
497
498 while (Buf != End) {
499 unsigned NumEntries = read_vbr_uint(Buf, End);
500 unsigned Ty = read_vbr_uint(Buf, End);
501
502 if (Ty >= CompactionTable.size())
503 CompactionTable.resize(Ty+1);
504
505 if (!CompactionTable[Ty].empty())
506 throw std::string("Compaction table plane contains multiple entries!");
507
508 if (Ty == Type::TypeTyID) {
509 for (unsigned i = 0; i != NumEntries; ++i) {
510 const Type *Typ = getGlobalTableType(read_vbr_uint(Buf, End));
511 CompactionTable[Type::TypeTyID].push_back(const_cast<Type*>(Typ));
512 }
513
514 CompactionTable.resize(NumEntries+Type::FirstDerivedTyID);
515 } else {
516 assert(NumEntries != 0 && "Cannot read zero entries!");
517 const Type *Typ = getType(Ty);
518 // Push the implicit zero
519 CompactionTable[Ty].push_back(Constant::getNullValue(Typ));
520 for (unsigned i = 1; i != NumEntries; ++i) {
521 Value *V = getGlobalTableValue(Typ, read_vbr_uint(Buf, End));
522 CompactionTable[Ty].push_back(V);
523 }
524 }
525 }
526
527}
528
529
530
Misha Brukman12c29d12003-09-22 23:38:23 +0000531void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
532 const unsigned char *End) {
533 if (!FunctionSignatureList.empty())
534 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000535
Chris Lattner70cc3392001-09-10 07:58:01 +0000536 // Read global variables...
Chris Lattner7969dc22004-01-15 06:13:09 +0000537 unsigned VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000538 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000539 unsigned SlotNo;
540 GlobalValue::LinkageTypes Linkage;
541
Chris Lattnerc08912f2004-01-14 16:44:44 +0000542 unsigned LinkageID;
543 if (hasExtendedLinkageSpecs) {
544 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
545 // bit2,3,4 = Linkage, bit4+ = slot#
546 SlotNo = VarType >> 5;
547 LinkageID = (VarType >> 2) & 7;
Chris Lattnere3869c82003-04-16 21:16:05 +0000548 } else {
549 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
Chris Lattnerc08912f2004-01-14 16:44:44 +0000550 // bit2,3 = Linkage, bit4+ = slot#
551 SlotNo = VarType >> 4;
552 LinkageID = (VarType >> 2) & 3;
553 }
554 switch (LinkageID) {
555 default: assert(0 && "Unknown linkage type!");
556 case 0: Linkage = GlobalValue::ExternalLinkage; break;
557 case 1: Linkage = GlobalValue::WeakLinkage; break;
558 case 2: Linkage = GlobalValue::AppendingLinkage; break;
559 case 3: Linkage = GlobalValue::InternalLinkage; break;
560 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattnere3869c82003-04-16 21:16:05 +0000561 }
562
563 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000564 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000565 throw std::string("Global not pointer type! Ty = " +
566 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000567
Chris Lattner52e20b02003-03-19 20:54:26 +0000568 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000569
Chris Lattner70cc3392001-09-10 07:58:01 +0000570 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000571 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000572 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000573 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000574 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +0000575
Chris Lattner7969dc22004-01-15 06:13:09 +0000576 if (VarType & 2) // Does it have an initializer?
577 GlobalInits.push_back(std::make_pair(GV, read_vbr_uint(Buf, End)));
578 VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000579 }
580
Chris Lattner52e20b02003-03-19 20:54:26 +0000581 // Read the function objects for all of the functions that are coming
Chris Lattner7969dc22004-01-15 06:13:09 +0000582 unsigned FnSignature = read_vbr_uint(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000583 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
584 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000585 if (!isa<PointerType>(Ty) ||
586 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000587 throw std::string("Function not ptr to func type! Ty = " +
588 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000589
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000590 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000591 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000592
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000593 // When the ModuleGlobalInfo section is read, we load the type of each
594 // function and the 'ModuleValues' slot that it lands in. We then load a
595 // placeholder into its slot to reserve it. When the function is loaded,
596 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000597
598 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000599 Function *Func = new Function(cast<FunctionType>(Ty),
600 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +0000601 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000602
Chris Lattner52e20b02003-03-19 20:54:26 +0000603 // Keep track of this information in a list that is emptied as functions are
604 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000605 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000606 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +0000607
Chris Lattner7969dc22004-01-15 06:13:09 +0000608 FnSignature = read_vbr_uint(Buf, End);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000609 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000610 }
611
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000612 if (hasInconsistentModuleGlobalInfo)
613 align32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000614
615 // Now that the function signature list is set up, reverse it so that we can
616 // remove elements efficiently from the back of the vector.
617 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000618
619 // This is for future proofing... in the future extra fields may be added that
620 // we don't understand, so we transparently ignore them.
621 //
622 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000623}
624
Misha Brukman12c29d12003-09-22 23:38:23 +0000625void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000626 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000627 unsigned Version = read_vbr_uint(Buf, EndBuf);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000628
629 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000630 Module::Endianness Endianness;
631 Module::PointerSize PointerSize;
632 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
633 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
634
635 bool hasNoEndianness = Version & 4;
636 bool hasNoPointerSize = Version & 8;
637
638 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000639
640 // Default values for the current bytecode version
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000641 hasExtendedLinkageSpecs = true;
642 hasOldStyleVarargs = false;
643 hasVarArgCallPadding = false;
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000644 hasInconsistentModuleGlobalInfo = false;
Chris Lattner80b97342004-01-17 23:25:43 +0000645 hasExplicitPrimitiveZeros = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000646
647 switch (RevisionNum) {
Chris Lattnerc08912f2004-01-14 16:44:44 +0000648 case 2: // LLVM pre-1.0 release: will be deleted on the next rev
Chris Lattner9e893e82004-01-14 23:35:21 +0000649 // Version #2 only supported 4 linkage types. It didn't support weak
650 // linkage.
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000651 hasExtendedLinkageSpecs = false;
652 hasOldStyleVarargs = true;
653 hasVarArgCallPadding = true;
Chris Lattner80b97342004-01-17 23:25:43 +0000654 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +0000655 case 0: // LLVM 1.0, 1.1 release version
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000656 // Compared to rev #2, we added support for weak linkage, a more dense
657 // encoding, and better varargs support.
Chris Lattner9e893e82004-01-14 23:35:21 +0000658
659 // Base LLVM 1.0 bytecode format.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000660 hasInconsistentModuleGlobalInfo = true;
Chris Lattner80b97342004-01-17 23:25:43 +0000661 hasExplicitPrimitiveZeros = true;
662 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +0000663 case 1: // LLVM 1.2 release version
Chris Lattner9e893e82004-01-14 23:35:21 +0000664 // LLVM 1.2 added explicit support for emitting strings efficiently.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000665
666 // Also, it fixed the problem where the size of the ModuleGlobalInfo block
667 // included the size for the alignment at the end, where the rest of the
668 // blocks did not.
Chris Lattnerc08912f2004-01-14 16:44:44 +0000669 break;
670
Chris Lattner036b8aa2003-03-06 17:55:45 +0000671 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000672 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000673 }
674
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000675 if (hasNoEndianness) Endianness = Module::AnyEndianness;
676 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000677
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000678 TheModule->setEndianness(Endianness);
679 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000680 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000681 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
682 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000683}
684
Misha Brukman12c29d12003-09-22 23:38:23 +0000685void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000686 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000687 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000688 readBlock(Buf, EndBuf, Type, Size);
689 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
690 throw std::string("Expected Module packet! B: "+
691 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
692 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattner1d670cc2001-09-07 16:37:43 +0000694 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000695 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000696
697 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000698 ParseVersionInfo(Buf, EndBuf);
Chris Lattner7969dc22004-01-15 06:13:09 +0000699 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000700
Chris Lattner00950542001-06-06 20:29:01 +0000701 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000702 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000703 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000704 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000705 case BytecodeFormat::GlobalTypePlane:
706 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000707 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000708 break;
709
710 case BytecodeFormat::ModuleGlobalInfo:
711 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000712 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000713 break;
714
Chris Lattner1d670cc2001-09-07 16:37:43 +0000715 case BytecodeFormat::ConstantPool:
716 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000717 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000718 break;
719
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000720 case BytecodeFormat::Function: {
721 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000722 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000723 break;
724 }
725
726 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000727 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000728 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000729 break;
Chris Lattner00950542001-06-06 20:29:01 +0000730 default:
Chris Lattner00950542001-06-06 20:29:01 +0000731 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000732 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000733 break;
734 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000735 BCR_TRACE(1, "} end block\n");
Chris Lattner7969dc22004-01-15 06:13:09 +0000736 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000737 }
738
Chris Lattner52e20b02003-03-19 20:54:26 +0000739 // After the module constant pool has been read, we can safely initialize
740 // global variables...
741 while (!GlobalInits.empty()) {
742 GlobalVariable *GV = GlobalInits.back().first;
743 unsigned Slot = GlobalInits.back().second;
744 GlobalInits.pop_back();
745
746 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +0000747 // FIXME: Preserve this type ID!
748 unsigned TypeSlot = getTypeSlot(GV->getType()->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +0000749 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000750 if (GV->hasInitializer())
751 throw std::string("Global *already* has an initializer?!");
Chris Lattner93361992004-01-15 18:45:25 +0000752 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +0000753 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000754 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000755 }
756
Misha Brukman12c29d12003-09-22 23:38:23 +0000757 if (!FunctionSignatureList.empty())
758 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000759
760 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000761}
762
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000763void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
764 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000765
Misha Brukman12c29d12003-09-22 23:38:23 +0000766 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000767
Chris Lattner00950542001-06-06 20:29:01 +0000768 // Read and check signature...
Chris Lattner7969dc22004-01-15 06:13:09 +0000769 unsigned Sig = read(Buf, EndBuf);
770 if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
Misha Brukman12c29d12003-09-22 23:38:23 +0000771 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000772
Chris Lattner75f20532003-04-22 18:02:52 +0000773 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000774 try {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000775 usesOldStyleVarargs = false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000776 ParseModule(Buf, EndBuf);
777 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000778 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000779 delete TheModule;
780 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000781 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000782 }
Chris Lattner00950542001-06-06 20:29:01 +0000783}