blob: f4b620ed19a0305d8cad922fc2a25b06b5b641d5 [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) {
Chris Lattner24102432004-01-18 22:35:34 +0000499 unsigned NumEntries;
500 unsigned Ty;
501
502 NumEntries = read_vbr_uint(Buf, End);
503 switch (NumEntries & 3) {
504 case 0:
505 case 1:
506 case 2:
507 Ty = NumEntries >> 2;
508 NumEntries &= 3;
509 break;
510 case 3:
511 NumEntries >>= 2;
512 Ty = read_vbr_uint(Buf, End);
513 break;
514 }
Chris Lattner89e02532004-01-18 21:08:15 +0000515
516 if (Ty >= CompactionTable.size())
517 CompactionTable.resize(Ty+1);
518
519 if (!CompactionTable[Ty].empty())
520 throw std::string("Compaction table plane contains multiple entries!");
521
522 if (Ty == Type::TypeTyID) {
523 for (unsigned i = 0; i != NumEntries; ++i) {
524 const Type *Typ = getGlobalTableType(read_vbr_uint(Buf, End));
525 CompactionTable[Type::TypeTyID].push_back(const_cast<Type*>(Typ));
526 }
527
528 CompactionTable.resize(NumEntries+Type::FirstDerivedTyID);
529 } else {
Chris Lattner89e02532004-01-18 21:08:15 +0000530 const Type *Typ = getType(Ty);
531 // Push the implicit zero
532 CompactionTable[Ty].push_back(Constant::getNullValue(Typ));
Chris Lattner24102432004-01-18 22:35:34 +0000533 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner89e02532004-01-18 21:08:15 +0000534 Value *V = getGlobalTableValue(Typ, read_vbr_uint(Buf, End));
535 CompactionTable[Ty].push_back(V);
536 }
537 }
538 }
539
540}
541
542
543
Misha Brukman12c29d12003-09-22 23:38:23 +0000544void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
545 const unsigned char *End) {
546 if (!FunctionSignatureList.empty())
547 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000548
Chris Lattner70cc3392001-09-10 07:58:01 +0000549 // Read global variables...
Chris Lattner7969dc22004-01-15 06:13:09 +0000550 unsigned VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000551 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000552 unsigned SlotNo;
553 GlobalValue::LinkageTypes Linkage;
554
Chris Lattnerc08912f2004-01-14 16:44:44 +0000555 unsigned LinkageID;
556 if (hasExtendedLinkageSpecs) {
557 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
558 // bit2,3,4 = Linkage, bit4+ = slot#
559 SlotNo = VarType >> 5;
560 LinkageID = (VarType >> 2) & 7;
Chris Lattnere3869c82003-04-16 21:16:05 +0000561 } else {
562 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
Chris Lattnerc08912f2004-01-14 16:44:44 +0000563 // bit2,3 = Linkage, bit4+ = slot#
564 SlotNo = VarType >> 4;
565 LinkageID = (VarType >> 2) & 3;
566 }
567 switch (LinkageID) {
568 default: assert(0 && "Unknown linkage type!");
569 case 0: Linkage = GlobalValue::ExternalLinkage; break;
570 case 1: Linkage = GlobalValue::WeakLinkage; break;
571 case 2: Linkage = GlobalValue::AppendingLinkage; break;
572 case 3: Linkage = GlobalValue::InternalLinkage; break;
573 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattnere3869c82003-04-16 21:16:05 +0000574 }
575
576 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000577 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000578 throw std::string("Global not pointer type! Ty = " +
579 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000580
Chris Lattner52e20b02003-03-19 20:54:26 +0000581 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000582
Chris Lattner70cc3392001-09-10 07:58:01 +0000583 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000584 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000585 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000586 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000587 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +0000588
Chris Lattner7969dc22004-01-15 06:13:09 +0000589 if (VarType & 2) // Does it have an initializer?
590 GlobalInits.push_back(std::make_pair(GV, read_vbr_uint(Buf, End)));
591 VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000592 }
593
Chris Lattner52e20b02003-03-19 20:54:26 +0000594 // Read the function objects for all of the functions that are coming
Chris Lattner7969dc22004-01-15 06:13:09 +0000595 unsigned FnSignature = read_vbr_uint(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000596 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
597 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000598 if (!isa<PointerType>(Ty) ||
599 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000600 throw std::string("Function not ptr to func type! Ty = " +
601 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000602
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000603 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000604 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000605
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000606 // When the ModuleGlobalInfo section is read, we load the type of each
607 // function and the 'ModuleValues' slot that it lands in. We then load a
608 // placeholder into its slot to reserve it. When the function is loaded,
609 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000610
611 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000612 Function *Func = new Function(cast<FunctionType>(Ty),
613 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +0000614 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000615
Chris Lattner52e20b02003-03-19 20:54:26 +0000616 // Keep track of this information in a list that is emptied as functions are
617 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000618 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000619 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +0000620
Chris Lattner7969dc22004-01-15 06:13:09 +0000621 FnSignature = read_vbr_uint(Buf, End);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000622 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000623 }
624
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000625 if (hasInconsistentModuleGlobalInfo)
626 align32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000627
628 // Now that the function signature list is set up, reverse it so that we can
629 // remove elements efficiently from the back of the vector.
630 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000631
632 // This is for future proofing... in the future extra fields may be added that
633 // we don't understand, so we transparently ignore them.
634 //
635 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000636}
637
Misha Brukman12c29d12003-09-22 23:38:23 +0000638void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000639 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000640 unsigned Version = read_vbr_uint(Buf, EndBuf);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000641
642 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000643 Module::Endianness Endianness;
644 Module::PointerSize PointerSize;
645 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
646 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
647
648 bool hasNoEndianness = Version & 4;
649 bool hasNoPointerSize = Version & 8;
650
651 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000652
653 // Default values for the current bytecode version
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000654 hasExtendedLinkageSpecs = true;
655 hasOldStyleVarargs = false;
656 hasVarArgCallPadding = false;
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000657 hasInconsistentModuleGlobalInfo = false;
Chris Lattner80b97342004-01-17 23:25:43 +0000658 hasExplicitPrimitiveZeros = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000659
660 switch (RevisionNum) {
Chris Lattnerc08912f2004-01-14 16:44:44 +0000661 case 2: // LLVM pre-1.0 release: will be deleted on the next rev
Chris Lattner9e893e82004-01-14 23:35:21 +0000662 // Version #2 only supported 4 linkage types. It didn't support weak
663 // linkage.
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000664 hasExtendedLinkageSpecs = false;
665 hasOldStyleVarargs = true;
666 hasVarArgCallPadding = true;
Chris Lattner80b97342004-01-17 23:25:43 +0000667 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +0000668 case 0: // LLVM 1.0, 1.1 release version
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000669 // Compared to rev #2, we added support for weak linkage, a more dense
670 // encoding, and better varargs support.
Chris Lattner9e893e82004-01-14 23:35:21 +0000671
672 // Base LLVM 1.0 bytecode format.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000673 hasInconsistentModuleGlobalInfo = true;
Chris Lattner80b97342004-01-17 23:25:43 +0000674 hasExplicitPrimitiveZeros = true;
675 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +0000676 case 1: // LLVM 1.2 release version
Chris Lattner9e893e82004-01-14 23:35:21 +0000677 // LLVM 1.2 added explicit support for emitting strings efficiently.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000678
679 // Also, it fixed the problem where the size of the ModuleGlobalInfo block
680 // included the size for the alignment at the end, where the rest of the
681 // blocks did not.
Chris Lattnerc08912f2004-01-14 16:44:44 +0000682 break;
683
Chris Lattner036b8aa2003-03-06 17:55:45 +0000684 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000685 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000686 }
687
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000688 if (hasNoEndianness) Endianness = Module::AnyEndianness;
689 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000690
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000691 TheModule->setEndianness(Endianness);
692 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000693 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000694 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
695 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000696}
697
Misha Brukman12c29d12003-09-22 23:38:23 +0000698void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000699 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000700 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000701 readBlock(Buf, EndBuf, Type, Size);
702 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
703 throw std::string("Expected Module packet! B: "+
704 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
705 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000706
Chris Lattner1d670cc2001-09-07 16:37:43 +0000707 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000708 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000709
710 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000711 ParseVersionInfo(Buf, EndBuf);
Chris Lattner7969dc22004-01-15 06:13:09 +0000712 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000713
Chris Lattner00950542001-06-06 20:29:01 +0000714 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000715 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000716 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000717 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000718 case BytecodeFormat::GlobalTypePlane:
719 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000720 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000721 break;
722
723 case BytecodeFormat::ModuleGlobalInfo:
724 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000725 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000726 break;
727
Chris Lattner1d670cc2001-09-07 16:37:43 +0000728 case BytecodeFormat::ConstantPool:
729 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000730 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000731 break;
732
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000733 case BytecodeFormat::Function: {
734 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000735 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000736 break;
737 }
738
739 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000740 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000741 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000742 break;
Chris Lattner00950542001-06-06 20:29:01 +0000743 default:
Chris Lattner00950542001-06-06 20:29:01 +0000744 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000745 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000746 break;
747 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000748 BCR_TRACE(1, "} end block\n");
Chris Lattner7969dc22004-01-15 06:13:09 +0000749 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000750 }
751
Chris Lattner52e20b02003-03-19 20:54:26 +0000752 // After the module constant pool has been read, we can safely initialize
753 // global variables...
754 while (!GlobalInits.empty()) {
755 GlobalVariable *GV = GlobalInits.back().first;
756 unsigned Slot = GlobalInits.back().second;
757 GlobalInits.pop_back();
758
759 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +0000760 // FIXME: Preserve this type ID!
761 unsigned TypeSlot = getTypeSlot(GV->getType()->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +0000762 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000763 if (GV->hasInitializer())
764 throw std::string("Global *already* has an initializer?!");
Chris Lattner93361992004-01-15 18:45:25 +0000765 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +0000766 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000767 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000768 }
769
Misha Brukman12c29d12003-09-22 23:38:23 +0000770 if (!FunctionSignatureList.empty())
771 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000772
773 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000774}
775
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000776void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
777 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000778
Misha Brukman12c29d12003-09-22 23:38:23 +0000779 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000780
Chris Lattner00950542001-06-06 20:29:01 +0000781 // Read and check signature...
Chris Lattner7969dc22004-01-15 06:13:09 +0000782 unsigned Sig = read(Buf, EndBuf);
783 if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
Misha Brukman12c29d12003-09-22 23:38:23 +0000784 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000785
Chris Lattner75f20532003-04-22 18:02:52 +0000786 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000787 try {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000788 usesOldStyleVarargs = false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000789 ParseModule(Buf, EndBuf);
790 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000791 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000792 delete TheModule;
793 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000794 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000795 }
Chris Lattner00950542001-06-06 20:29:01 +0000796}