blob: 2f0879ba3943323c4e83899d7a23b3ca05ff7259 [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 Lattner52f86d62004-01-20 00:54:06 +0000123 // If the type plane was compactified, figure out the global type ID.
124 unsigned GlobalTyID = type;
125 if (CompactionTable.size() > Type::TypeTyID &&
126 !CompactionTable[Type::TypeTyID].empty() &&
127 type >= Type::FirstDerivedTyID) {
128 std::vector<Value*> &TypePlane = CompactionTable[Type::TypeTyID];
129 const Type *Ty = cast<Type>(TypePlane[type-Type::FirstDerivedTyID]);
130 TypeValuesListTy::iterator I =
131 find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
132 assert(I != ModuleTypeValues.end());
133 GlobalTyID = Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
134 }
Chris Lattner00950542001-06-06 20:29:01 +0000135
Chris Lattner52f86d62004-01-20 00:54:06 +0000136 if (hasImplicitNull(GlobalTyID, hasExplicitPrimitiveZeros)) {
Chris Lattner89e02532004-01-18 21:08:15 +0000137 if (Num == 0)
138 return Constant::getNullValue(getType(type));
139 --Num;
140 }
141
Chris Lattner52f86d62004-01-20 00:54:06 +0000142 if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
143 if (Num < ModuleValues[GlobalTyID]->size())
144 return ModuleValues[GlobalTyID]->getOperand(Num);
145 Num -= ModuleValues[GlobalTyID]->size();
Chris Lattner89e02532004-01-18 21:08:15 +0000146 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000147 }
148
Chris Lattner29b789b2003-11-19 17:27:18 +0000149 if (Values.size() > type && Values[type] && Num < Values[type]->size())
Chris Lattner52e20b02003-03-19 20:54:26 +0000150 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner74734132002-08-17 22:01:27 +0000152 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000154 std::pair<unsigned,unsigned> KeyValue(type, oNum);
155 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
156 ForwardReferences.lower_bound(KeyValue);
157 if (I != ForwardReferences.end() && I->first == KeyValue)
158 return I->second; // We have already created this placeholder
159
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000160 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000161 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000162 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000163}
164
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000165/// getBasicBlock - Get a particular numbered basic block, which might be a
166/// forward reference. This works together with ParseBasicBlock to handle these
167/// forward references in a clean manner.
168///
169BasicBlock *BytecodeParser::getBasicBlock(unsigned ID) {
170 // Make sure there is room in the table...
171 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
172
173 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
174 // has already created this block, or if the forward reference has already
175 // been created.
176 if (ParsedBasicBlocks[ID])
177 return ParsedBasicBlocks[ID];
178
179 // Otherwise, the basic block has not yet been created. Do so and add it to
180 // the ParsedBasicBlocks list.
181 return ParsedBasicBlocks[ID] = new BasicBlock();
182}
183
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000184/// getConstantValue - Just like getValue, except that it returns a null pointer
185/// only on error. It always returns a constant (meaning that if the value is
186/// defined, but is not a constant, that is an error). If the specified
187/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
188/// after the real value is parsed, the placeholder is eliminated.
189///
Chris Lattner1c3673b2003-11-19 06:01:12 +0000190Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
191 if (Value *V = getValue(TypeSlot, Slot, false))
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000192 if (Constant *C = dyn_cast<Constant>(V))
193 return C; // If we already have the value parsed, just return it
Chris Lattner93361992004-01-15 18:45:25 +0000194 else if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
195 // ConstantPointerRef's are an abomination, but at least they don't have
196 // to infest bytecode files.
197 return ConstantPointerRef::get(GV);
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000198 else
199 throw std::string("Reference of a value is expected to be a constant!");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000200
Chris Lattner1c3673b2003-11-19 06:01:12 +0000201 const Type *Ty = getType(TypeSlot);
Chris Lattner52e20b02003-03-19 20:54:26 +0000202 std::pair<const Type*, unsigned> Key(Ty, Slot);
Chris Lattner29b789b2003-11-19 17:27:18 +0000203 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
Chris Lattner52e20b02003-03-19 20:54:26 +0000204
Chris Lattner29b789b2003-11-19 17:27:18 +0000205 if (I != ConstantFwdRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000206 BCR_TRACE(5, "Previous forward ref found!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000207 return I->second;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000208 } else {
209 // Create a placeholder for the constant reference and
210 // keep track of the fact that we have a forward ref to recycle it
211 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
212 Constant *C = new ConstPHolder(Ty, Slot);
213
214 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner29b789b2003-11-19 17:27:18 +0000215 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000216 return C;
217 }
218}
219
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000220/// ParseBasicBlock - In LLVM 1.0 bytecode files, we used to output one
221/// basicblock at a time. This method reads in one of the basicblock packets.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000222BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
223 const unsigned char *EndBuf,
224 unsigned BlockNo) {
225 BasicBlock *BB;
226 if (ParsedBasicBlocks.size() == BlockNo)
227 ParsedBasicBlocks.push_back(BB = new BasicBlock());
228 else if (ParsedBasicBlocks[BlockNo] == 0)
229 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
230 else
231 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000232
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000233 std::vector<unsigned> Args;
234 while (Buf < EndBuf)
235 ParseInstruction(Buf, EndBuf, Args, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000236
Misha Brukman12c29d12003-09-22 23:38:23 +0000237 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000238}
239
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000240
241/// ParseInstructionList - Parse all of the BasicBlock's & Instruction's in the
242/// body of a function. In post 1.0 bytecode files, we no longer emit basic
243/// block individually, in order to avoid per-basic-block overhead.
244unsigned BytecodeParser::ParseInstructionList(Function *F,
245 const unsigned char *&Buf,
246 const unsigned char *EndBuf) {
247 unsigned BlockNo = 0;
248 std::vector<unsigned> Args;
249
250 while (Buf < EndBuf) {
251 BasicBlock *BB;
252 if (ParsedBasicBlocks.size() == BlockNo)
253 ParsedBasicBlocks.push_back(BB = new BasicBlock());
254 else if (ParsedBasicBlocks[BlockNo] == 0)
255 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
256 else
257 BB = ParsedBasicBlocks[BlockNo];
258 ++BlockNo;
259 F->getBasicBlockList().push_back(BB);
260
261 // Read instructions into this basic block until we get to a terminator
262 while (Buf < EndBuf && !BB->getTerminator())
263 ParseInstruction(Buf, EndBuf, Args, BB);
264
265 if (!BB->getTerminator())
266 throw std::string("Non-terminated basic block found!");
267 }
268
269 return BlockNo;
270}
271
Misha Brukman12c29d12003-09-22 23:38:23 +0000272void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000273 const unsigned char *EndBuf,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000274 SymbolTable *ST,
275 Function *CurrentFunction) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000276 // Allow efficient basic block lookup by number.
277 std::vector<BasicBlock*> BBMap;
278 if (CurrentFunction)
279 for (Function::iterator I = CurrentFunction->begin(),
280 E = CurrentFunction->end(); I != E; ++I)
281 BBMap.push_back(I);
282
Chris Lattner00950542001-06-06 20:29:01 +0000283 while (Buf < EndBuf) {
284 // Symtab block header: [num entries][type id number]
Chris Lattner7969dc22004-01-15 06:13:09 +0000285 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
286 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000287 const Type *Ty = getType(Typ);
Chris Lattner927b1852003-10-09 20:22:47 +0000288 BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000289 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000290
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000291 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000292 // Symtab entry: [def slot #][name]
Chris Lattner7969dc22004-01-15 06:13:09 +0000293 unsigned slot = read_vbr_uint(Buf, EndBuf);
294 std::string Name = read_str(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000295
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000296 Value *V = 0;
Chris Lattner36392bc2003-10-08 21:18:57 +0000297 if (Typ == Type::TypeTyID)
298 V = (Value*)getType(slot);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000299 else if (Typ == Type::LabelTyID) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000300 if (slot < BBMap.size())
301 V = BBMap[slot];
302 } else {
Chris Lattner36392bc2003-10-08 21:18:57 +0000303 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +0000304 }
Chris Lattner52f86d62004-01-20 00:54:06 +0000305 if (V == 0)
Chris Lattner11cad512004-03-29 00:16:01 +0000306 throw "Failed value look-up for name '" + Name + "'";
Chris Lattner52e20b02003-03-19 20:54:26 +0000307 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000308 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000309
Chris Lattner52e20b02003-03-19 20:54:26 +0000310 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
312 }
313
Misha Brukman12c29d12003-09-22 23:38:23 +0000314 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000315}
316
Chris Lattner29b789b2003-11-19 17:27:18 +0000317void BytecodeParser::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
318 ConstantRefsType::iterator I =
319 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
320 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000321
Chris Lattner74734132002-08-17 22:01:27 +0000322 BCR_TRACE(3, "Mutating forward refs!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000323 Value *PH = I->second; // Get the placeholder...
324 PH->replaceAllUsesWith(NewV);
325 delete PH; // Delete the old placeholder
326 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000327}
328
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000329void BytecodeParser::ParseFunction(const unsigned char *&Buf,
330 const unsigned char *EndBuf) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000331 if (FunctionSignatureList.empty())
332 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000333
Chris Lattner29b789b2003-11-19 17:27:18 +0000334 Function *F = FunctionSignatureList.back();
Misha Brukman12c29d12003-09-22 23:38:23 +0000335 FunctionSignatureList.pop_back();
336
337 // Save the information for future reading of the function
Chris Lattner29b789b2003-11-19 17:27:18 +0000338 LazyFunctionLoadMap[F] = LazyFunctionInfo(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000339 // Pretend we've `parsed' this function
340 Buf = EndBuf;
341}
342
343void BytecodeParser::materializeFunction(Function* F) {
344 // Find {start, end} pointers and slot in the map. If not there, we're done.
Chris Lattner29b789b2003-11-19 17:27:18 +0000345 std::map<Function*, LazyFunctionInfo>::iterator Fi =
Misha Brukman12c29d12003-09-22 23:38:23 +0000346 LazyFunctionLoadMap.find(F);
347 if (Fi == LazyFunctionLoadMap.end()) return;
Chris Lattner29b789b2003-11-19 17:27:18 +0000348
349 const unsigned char *Buf = Fi->second.Buf;
350 const unsigned char *EndBuf = Fi->second.EndBuf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000351 LazyFunctionLoadMap.erase(Fi);
Chris Lattner00950542001-06-06 20:29:01 +0000352
Chris Lattnere3869c82003-04-16 21:16:05 +0000353 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
354
Chris Lattner7969dc22004-01-15 06:13:09 +0000355 unsigned LinkageType = read_vbr_uint(Buf, EndBuf);
Chris Lattner9dd87702004-04-03 23:43:42 +0000356 if (LinkageType > 4)
Chris Lattnerc08912f2004-01-14 16:44:44 +0000357 throw std::string("Invalid linkage type for Function.");
358 switch (LinkageType) {
359 case 0: Linkage = GlobalValue::ExternalLinkage; break;
360 case 1: Linkage = GlobalValue::WeakLinkage; break;
361 case 2: Linkage = GlobalValue::AppendingLinkage; break;
362 case 3: Linkage = GlobalValue::InternalLinkage; break;
363 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattnere3869c82003-04-16 21:16:05 +0000364 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000365
Chris Lattnere3869c82003-04-16 21:16:05 +0000366 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000367
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000368 // Keep track of how many basic blocks we have read in...
369 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +0000370 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000371
Chris Lattner00950542001-06-06 20:29:01 +0000372 while (Buf < EndBuf) {
373 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000374 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000375 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000376
377 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +0000378 case BytecodeFormat::ConstantPool:
Chris Lattner89e02532004-01-18 21:08:15 +0000379 if (!InsertedArguments) {
380 // Insert arguments into the value table before we parse the first basic
381 // block in the function, but after we potentially read in the
382 // compaction table.
Chris Lattnerd5d89962004-02-09 04:14:01 +0000383 const FunctionType *FT = F->getFunctionType();
Chris Lattner89e02532004-01-18 21:08:15 +0000384 Function::aiterator AI = F->abegin();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000385 for (FunctionType::param_iterator It = FT->param_begin();
386 It != FT->param_end(); ++It, ++AI)
Chris Lattner89e02532004-01-18 21:08:15 +0000387 insertValue(AI, getTypeSlot(AI->getType()), Values);
388 InsertedArguments = true;
389 }
390
Chris Lattner1d670cc2001-09-07 16:37:43 +0000391 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000392 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000393 break;
394
Chris Lattner89e02532004-01-18 21:08:15 +0000395 case BytecodeFormat::CompactionTable:
396 BCR_TRACE(2, "BLOCK BytecodeFormat::CompactionTable: {\n");
397 ParseCompactionTable(Buf, Buf+Size);
398 break;
399
Chris Lattner00950542001-06-06 20:29:01 +0000400 case BytecodeFormat::BasicBlock: {
Chris Lattner89e02532004-01-18 21:08:15 +0000401 if (!InsertedArguments) {
402 // Insert arguments into the value table before we parse the first basic
403 // block in the function, but after we potentially read in the
404 // compaction table.
Chris Lattnerd5d89962004-02-09 04:14:01 +0000405 const FunctionType *FT = F->getFunctionType();
Chris Lattner89e02532004-01-18 21:08:15 +0000406 Function::aiterator AI = F->abegin();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000407 for (FunctionType::param_iterator It = FT->param_begin();
408 It != FT->param_end(); ++It, ++AI)
Chris Lattner89e02532004-01-18 21:08:15 +0000409 insertValue(AI, getTypeSlot(AI->getType()), Values);
410 InsertedArguments = true;
411 }
412
Chris Lattner1d670cc2001-09-07 16:37:43 +0000413 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000414 BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
415 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000416 break;
417 }
418
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000419 case BytecodeFormat::InstructionList: {
Chris Lattner89e02532004-01-18 21:08:15 +0000420 // Insert arguments into the value table before we parse the instruction
421 // list for the function, but after we potentially read in the compaction
422 // table.
423 if (!InsertedArguments) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000424 const FunctionType *FT = F->getFunctionType();
Chris Lattner89e02532004-01-18 21:08:15 +0000425 Function::aiterator AI = F->abegin();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000426 for (FunctionType::param_iterator It = FT->param_begin();
427 It != FT->param_end(); ++It, ++AI)
Chris Lattner89e02532004-01-18 21:08:15 +0000428 insertValue(AI, getTypeSlot(AI->getType()), Values);
429 InsertedArguments = true;
430 }
431
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000432 BCR_TRACE(2, "BLOCK BytecodeFormat::InstructionList: {\n");
433 if (BlockNum) throw std::string("Already parsed basic blocks!");
434 BlockNum = ParseInstructionList(F, Buf, Buf+Size);
435 break;
436 }
437
Chris Lattner29b789b2003-11-19 17:27:18 +0000438 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000439 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000440 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
Chris Lattner00950542001-06-06 20:29:01 +0000441 break;
442
443 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000444 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000445 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000446 if (OldBuf > Buf)
447 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000448 break;
449 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000450 BCR_TRACE(2, "} end block\n");
451
Misha Brukman12c29d12003-09-22 23:38:23 +0000452 // Malformed bc file if read past end of block.
Chris Lattner7969dc22004-01-15 06:13:09 +0000453 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000454 }
455
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000456 // Make sure there were no references to non-existant basic blocks.
457 if (BlockNum != ParsedBasicBlocks.size())
458 throw std::string("Illegal basic block operand reference");
459 ParsedBasicBlocks.clear();
460
Chris Lattner97330cf2003-10-09 23:10:14 +0000461 // Resolve forward references. Replace any uses of a forward reference value
462 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000463
Chris Lattner97330cf2003-10-09 23:10:14 +0000464 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
465 // number of operands. PHI nodes often have forward references, and can also
466 // often have a very large number of operands.
Chris Lattner89e02532004-01-18 21:08:15 +0000467 //
468 // FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code
469 // should be simplified back to using it!
470 //
Chris Lattner97330cf2003-10-09 23:10:14 +0000471 std::map<Value*, Value*> ForwardRefMapping;
472 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
473 I = ForwardReferences.begin(), E = ForwardReferences.end();
474 I != E; ++I)
475 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
476 false);
477
478 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
479 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
480 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
481 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
482 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
483 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
484 }
485
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000486 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +0000487 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
488 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000489 Value *PlaceHolder = I->second;
490 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +0000491
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000492 // Now that all the uses are gone, delete the placeholder...
493 // If we couldn't find a def (error case), then leak a little
494 // memory, because otherwise we can't remove all uses!
495 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +0000496 }
Chris Lattner00950542001-06-06 20:29:01 +0000497
Misha Brukman12c29d12003-09-22 23:38:23 +0000498 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000499 FunctionTypeValues.clear();
Chris Lattner89e02532004-01-18 21:08:15 +0000500 CompactionTable.clear();
Chris Lattner52e20b02003-03-19 20:54:26 +0000501 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000502}
503
Chris Lattner89e02532004-01-18 21:08:15 +0000504void BytecodeParser::ParseCompactionTable(const unsigned char *&Buf,
505 const unsigned char *End) {
506
507 while (Buf != End) {
Chris Lattnercdaff322004-01-20 17:06:29 +0000508 unsigned NumEntries = read_vbr_uint(Buf, End);
Chris Lattner24102432004-01-18 22:35:34 +0000509 unsigned Ty;
510
Chris Lattnercdaff322004-01-20 17:06:29 +0000511 if ((NumEntries & 3) == 3) {
Chris Lattner24102432004-01-18 22:35:34 +0000512 NumEntries >>= 2;
513 Ty = read_vbr_uint(Buf, End);
Chris Lattnercdaff322004-01-20 17:06:29 +0000514 } else {
515 Ty = NumEntries >> 2;
516 NumEntries &= 3;
Chris Lattner24102432004-01-18 22:35:34 +0000517 }
Chris Lattner89e02532004-01-18 21:08:15 +0000518
519 if (Ty >= CompactionTable.size())
520 CompactionTable.resize(Ty+1);
521
522 if (!CompactionTable[Ty].empty())
523 throw std::string("Compaction table plane contains multiple entries!");
524
525 if (Ty == Type::TypeTyID) {
526 for (unsigned i = 0; i != NumEntries; ++i) {
527 const Type *Typ = getGlobalTableType(read_vbr_uint(Buf, End));
528 CompactionTable[Type::TypeTyID].push_back(const_cast<Type*>(Typ));
529 }
530
531 CompactionTable.resize(NumEntries+Type::FirstDerivedTyID);
532 } else {
Chris Lattner89e02532004-01-18 21:08:15 +0000533 const Type *Typ = getType(Ty);
534 // Push the implicit zero
535 CompactionTable[Ty].push_back(Constant::getNullValue(Typ));
Chris Lattner24102432004-01-18 22:35:34 +0000536 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner89e02532004-01-18 21:08:15 +0000537 Value *V = getGlobalTableValue(Typ, read_vbr_uint(Buf, End));
538 CompactionTable[Ty].push_back(V);
539 }
540 }
541 }
542
543}
544
545
546
Misha Brukman12c29d12003-09-22 23:38:23 +0000547void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
548 const unsigned char *End) {
549 if (!FunctionSignatureList.empty())
550 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000551
Chris Lattner70cc3392001-09-10 07:58:01 +0000552 // Read global variables...
Chris Lattner7969dc22004-01-15 06:13:09 +0000553 unsigned VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000554 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattner9dd87702004-04-03 23:43:42 +0000555 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
556 // Linkage, bit4+ = slot#
557 unsigned SlotNo = VarType >> 5;
558 unsigned LinkageID = (VarType >> 2) & 7;
Chris Lattnere3869c82003-04-16 21:16:05 +0000559 GlobalValue::LinkageTypes Linkage;
560
Chris Lattnerc08912f2004-01-14 16:44:44 +0000561 switch (LinkageID) {
562 default: assert(0 && "Unknown linkage type!");
563 case 0: Linkage = GlobalValue::ExternalLinkage; break;
564 case 1: Linkage = GlobalValue::WeakLinkage; break;
565 case 2: Linkage = GlobalValue::AppendingLinkage; break;
566 case 3: Linkage = GlobalValue::InternalLinkage; break;
567 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattnere3869c82003-04-16 21:16:05 +0000568 }
569
570 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000571 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000572 throw std::string("Global not pointer type! Ty = " +
573 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000574
Chris Lattner52e20b02003-03-19 20:54:26 +0000575 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000576
Chris Lattner70cc3392001-09-10 07:58:01 +0000577 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000578 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000579 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000580 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000581 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +0000582
Chris Lattner7969dc22004-01-15 06:13:09 +0000583 if (VarType & 2) // Does it have an initializer?
584 GlobalInits.push_back(std::make_pair(GV, read_vbr_uint(Buf, End)));
585 VarType = read_vbr_uint(Buf, End);
Chris Lattner70cc3392001-09-10 07:58:01 +0000586 }
587
Chris Lattner52e20b02003-03-19 20:54:26 +0000588 // Read the function objects for all of the functions that are coming
Chris Lattner7969dc22004-01-15 06:13:09 +0000589 unsigned FnSignature = read_vbr_uint(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000590 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
591 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000592 if (!isa<PointerType>(Ty) ||
593 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000594 throw std::string("Function not ptr to func type! Ty = " +
595 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000596
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000597 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000598 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000599
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000600 // When the ModuleGlobalInfo section is read, we load the type of each
601 // function and the 'ModuleValues' slot that it lands in. We then load a
602 // placeholder into its slot to reserve it. When the function is loaded,
603 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000604
605 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000606 Function *Func = new Function(cast<FunctionType>(Ty),
607 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +0000608 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000609
Chris Lattner52e20b02003-03-19 20:54:26 +0000610 // Keep track of this information in a list that is emptied as functions are
611 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000612 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000613 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +0000614
Chris Lattner7969dc22004-01-15 06:13:09 +0000615 FnSignature = read_vbr_uint(Buf, End);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000616 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000617 }
618
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000619 if (hasInconsistentModuleGlobalInfo)
620 align32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000621
622 // Now that the function signature list is set up, reverse it so that we can
623 // remove elements efficiently from the back of the vector.
624 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000625
626 // This is for future proofing... in the future extra fields may be added that
627 // we don't understand, so we transparently ignore them.
628 //
629 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000630}
631
Misha Brukman12c29d12003-09-22 23:38:23 +0000632void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000633 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000634 unsigned Version = read_vbr_uint(Buf, EndBuf);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000635
636 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000637 Module::Endianness Endianness;
638 Module::PointerSize PointerSize;
639 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
640 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
641
642 bool hasNoEndianness = Version & 4;
643 bool hasNoPointerSize = Version & 8;
644
645 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000646
647 // Default values for the current bytecode version
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000648 hasInconsistentModuleGlobalInfo = false;
Chris Lattner80b97342004-01-17 23:25:43 +0000649 hasExplicitPrimitiveZeros = false;
Chris Lattner5fa428f2004-04-05 01:27:26 +0000650 hasRestrictedGEPTypes = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000651
652 switch (RevisionNum) {
Chris Lattnerc08912f2004-01-14 16:44:44 +0000653 case 0: // LLVM 1.0, 1.1 release version
Chris Lattner9e893e82004-01-14 23:35:21 +0000654 // Base LLVM 1.0 bytecode format.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000655 hasInconsistentModuleGlobalInfo = true;
Chris Lattner80b97342004-01-17 23:25:43 +0000656 hasExplicitPrimitiveZeros = true;
657 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +0000658 case 1: // LLVM 1.2 release version
Chris Lattner9e893e82004-01-14 23:35:21 +0000659 // LLVM 1.2 added explicit support for emitting strings efficiently.
Chris Lattner44d0eeb2004-01-15 17:55:01 +0000660
661 // Also, it fixed the problem where the size of the ModuleGlobalInfo block
662 // included the size for the alignment at the end, where the rest of the
663 // blocks did not.
Chris Lattner5fa428f2004-04-05 01:27:26 +0000664
665 // LLVM 1.2 and before required that GEP indices be ubyte constants for
666 // structures and longs for sequential types.
667 hasRestrictedGEPTypes = true;
668
669 // FALL THROUGH
670 case 2: // LLVM 1.3 release version
Chris Lattnerc08912f2004-01-14 16:44:44 +0000671 break;
672
Chris Lattner036b8aa2003-03-06 17:55:45 +0000673 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000674 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000675 }
676
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000677 if (hasNoEndianness) Endianness = Module::AnyEndianness;
678 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000679
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000680 TheModule->setEndianness(Endianness);
681 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000682 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000683 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
684 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000685}
686
Misha Brukman12c29d12003-09-22 23:38:23 +0000687void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000688 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000689 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000690 readBlock(Buf, EndBuf, Type, Size);
691 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
692 throw std::string("Expected Module packet! B: "+
693 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
694 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000695
Chris Lattner1d670cc2001-09-07 16:37:43 +0000696 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000697 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000698
699 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000700 ParseVersionInfo(Buf, EndBuf);
Chris Lattner7969dc22004-01-15 06:13:09 +0000701 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000702
Chris Lattner00950542001-06-06 20:29:01 +0000703 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000704 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000705 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000706 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000707 case BytecodeFormat::GlobalTypePlane:
708 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000709 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000710 break;
711
712 case BytecodeFormat::ModuleGlobalInfo:
713 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000714 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000715 break;
716
Chris Lattner1d670cc2001-09-07 16:37:43 +0000717 case BytecodeFormat::ConstantPool:
718 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000719 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000720 break;
721
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000722 case BytecodeFormat::Function: {
723 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000724 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000725 break;
726 }
727
728 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000729 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000730 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000731 break;
Chris Lattner00950542001-06-06 20:29:01 +0000732 default:
Chris Lattner00950542001-06-06 20:29:01 +0000733 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000734 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000735 break;
736 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000737 BCR_TRACE(1, "} end block\n");
Chris Lattner7969dc22004-01-15 06:13:09 +0000738 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000739 }
740
Chris Lattner52e20b02003-03-19 20:54:26 +0000741 // After the module constant pool has been read, we can safely initialize
742 // global variables...
743 while (!GlobalInits.empty()) {
744 GlobalVariable *GV = GlobalInits.back().first;
745 unsigned Slot = GlobalInits.back().second;
746 GlobalInits.pop_back();
747
748 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +0000749 // FIXME: Preserve this type ID!
750 unsigned TypeSlot = getTypeSlot(GV->getType()->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +0000751 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000752 if (GV->hasInitializer())
753 throw std::string("Global *already* has an initializer?!");
Chris Lattner93361992004-01-15 18:45:25 +0000754 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +0000755 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000756 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000757 }
758
Misha Brukman12c29d12003-09-22 23:38:23 +0000759 if (!FunctionSignatureList.empty())
760 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000761
762 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000763}
764
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000765void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
766 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000767
Misha Brukman12c29d12003-09-22 23:38:23 +0000768 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000769
Chris Lattner00950542001-06-06 20:29:01 +0000770 // Read and check signature...
Chris Lattner7969dc22004-01-15 06:13:09 +0000771 unsigned Sig = read(Buf, EndBuf);
772 if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
Misha Brukman12c29d12003-09-22 23:38:23 +0000773 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000774
Chris Lattner75f20532003-04-22 18:02:52 +0000775 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000776 try {
777 ParseModule(Buf, EndBuf);
778 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000779 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000780 delete TheModule;
781 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000782 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000783 }
Chris Lattner00950542001-06-06 20:29:01 +0000784}