blob: 33e6fbe98b9035f95fe7eb75bcb3dacdae10787d [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
Misha Brukmane0dd0d42003-09-23 16:15:29 +000026static inline void ALIGN32(const unsigned char *&begin,
27 const unsigned char *end) {
28 if (align32(begin, end))
29 throw std::string("Alignment error in buffer: read past end of block.");
30}
Misha Brukman12c29d12003-09-22 23:38:23 +000031
Chris Lattner9e460f22003-10-04 20:00:03 +000032unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
33 if (Ty->isPrimitiveType())
34 return Ty->getPrimitiveID();
35
36 // Check the function level types first...
37 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
38 FunctionTypeValues.end(), Ty);
39 if (I != FunctionTypeValues.end())
40 return FirstDerivedTyID + ModuleTypeValues.size() +
41 (&*I - &FunctionTypeValues[0]);
42
43 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
44 if (I == ModuleTypeValues.end())
45 throw std::string("Didn't find type in ModuleTypeValues.");
46 return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000047}
48
49const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner927b1852003-10-09 20:22:47 +000050 if (ID < Type::NumPrimitiveIDs)
51 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
52 return T;
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattner697954c2002-01-20 22:54:45 +000054 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner36392bc2003-10-08 21:18:57 +000055
Chris Lattner927b1852003-10-09 20:22:47 +000056 if (ID < Type::NumPrimitiveIDs)
57 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
58 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +000059
60 // Otherwise, derived types need offset...
61 ID -= FirstDerivedTyID;
62
63 // Is it a module-level type?
64 if (ID < ModuleTypeValues.size())
65 return ModuleTypeValues[ID].get();
66
67 // Nope, is it a function-level type?
68 ID -= ModuleTypeValues.size();
69 if (ID < FunctionTypeValues.size())
70 return FunctionTypeValues[ID].get();
71
Chris Lattner927b1852003-10-09 20:22:47 +000072 throw std::string("Illegal type reference!");
Chris Lattner00950542001-06-06 20:29:01 +000073}
74
Chris Lattnerf0d92732003-10-13 14:34:59 +000075unsigned BytecodeParser::insertValue(Value *Val, unsigned type,
76 ValueTable &ValueTab) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +000077 assert((!isa<Constant>(Val) || Val->getType()->isPrimitiveType() ||
Chris Lattner52e20b02003-03-19 20:54:26 +000078 !cast<Constant>(Val)->isNullValue()) &&
79 "Cannot read null values from bytecode!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000080 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner29b789b2003-11-19 17:27:18 +000081
Chris Lattner52e20b02003-03-19 20:54:26 +000082 if (ValueTab.size() <= type) {
83 unsigned OldSize = ValueTab.size();
84 ValueTab.resize(type+1);
Chris Lattner036b8aa2003-03-06 17:55:45 +000085 }
Chris Lattner00950542001-06-06 20:29:01 +000086
Chris Lattner29b789b2003-11-19 17:27:18 +000087 if (!ValueTab[type]) ValueTab[type] = new ValueList();
88
Chris Lattner00950542001-06-06 20:29:01 +000089 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000090 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000091 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000092
Chris Lattnercb7e2e22003-10-18 05:54:18 +000093 bool HasOffset = !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +000094 return ValueTab[type]->size()-1 + HasOffset;
95}
96
97
Chris Lattner36392bc2003-10-08 21:18:57 +000098Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
99 assert(type != Type::TypeTyID && "getValue() cannot get types!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000100 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000101 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000102
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000103 if (type >= FirstDerivedTyID) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000104 if (Num == 0)
Chris Lattner36392bc2003-10-08 21:18:57 +0000105 return Constant::getNullValue(getType(type));
Chris Lattner52e20b02003-03-19 20:54:26 +0000106 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000107 }
108
Chris Lattner29b789b2003-11-19 17:27:18 +0000109 if (type < ModuleValues.size() && ModuleValues[type]) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000110 if (Num < ModuleValues[type]->size())
111 return ModuleValues[type]->getOperand(Num);
112 Num -= ModuleValues[type]->size();
113 }
114
Chris Lattner29b789b2003-11-19 17:27:18 +0000115 if (Values.size() > type && Values[type] && Num < Values[type]->size())
Chris Lattner52e20b02003-03-19 20:54:26 +0000116 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000117
Chris Lattner74734132002-08-17 22:01:27 +0000118 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000119
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000120 std::pair<unsigned,unsigned> KeyValue(type, oNum);
121 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
122 ForwardReferences.lower_bound(KeyValue);
123 if (I != ForwardReferences.end() && I->first == KeyValue)
124 return I->second; // We have already created this placeholder
125
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000126 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000127 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000128 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000129}
130
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000131/// getBasicBlock - Get a particular numbered basic block, which might be a
132/// forward reference. This works together with ParseBasicBlock to handle these
133/// forward references in a clean manner.
134///
135BasicBlock *BytecodeParser::getBasicBlock(unsigned ID) {
136 // Make sure there is room in the table...
137 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
138
139 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
140 // has already created this block, or if the forward reference has already
141 // been created.
142 if (ParsedBasicBlocks[ID])
143 return ParsedBasicBlocks[ID];
144
145 // Otherwise, the basic block has not yet been created. Do so and add it to
146 // the ParsedBasicBlocks list.
147 return ParsedBasicBlocks[ID] = new BasicBlock();
148}
149
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000150/// getConstantValue - Just like getValue, except that it returns a null pointer
151/// only on error. It always returns a constant (meaning that if the value is
152/// defined, but is not a constant, that is an error). If the specified
153/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
154/// after the real value is parsed, the placeholder is eliminated.
155///
Chris Lattner1c3673b2003-11-19 06:01:12 +0000156Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
157 if (Value *V = getValue(TypeSlot, Slot, false))
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000158 if (Constant *C = dyn_cast<Constant>(V))
159 return C; // If we already have the value parsed, just return it
160 else
161 throw std::string("Reference of a value is expected to be a constant!");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000162
Chris Lattner1c3673b2003-11-19 06:01:12 +0000163 const Type *Ty = getType(TypeSlot);
Chris Lattner52e20b02003-03-19 20:54:26 +0000164 std::pair<const Type*, unsigned> Key(Ty, Slot);
Chris Lattner29b789b2003-11-19 17:27:18 +0000165 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
Chris Lattner52e20b02003-03-19 20:54:26 +0000166
Chris Lattner29b789b2003-11-19 17:27:18 +0000167 if (I != ConstantFwdRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000168 BCR_TRACE(5, "Previous forward ref found!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000169 return I->second;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000170 } else {
171 // Create a placeholder for the constant reference and
172 // keep track of the fact that we have a forward ref to recycle it
173 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
174 Constant *C = new ConstPHolder(Ty, Slot);
175
176 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner29b789b2003-11-19 17:27:18 +0000177 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000178 return C;
179 }
180}
181
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000182/// ParseBasicBlock - In LLVM 1.0 bytecode files, we used to output one
183/// basicblock at a time. This method reads in one of the basicblock packets.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000184BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
185 const unsigned char *EndBuf,
186 unsigned BlockNo) {
187 BasicBlock *BB;
188 if (ParsedBasicBlocks.size() == BlockNo)
189 ParsedBasicBlocks.push_back(BB = new BasicBlock());
190 else if (ParsedBasicBlocks[BlockNo] == 0)
191 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
192 else
193 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000195 std::vector<unsigned> Args;
196 while (Buf < EndBuf)
197 ParseInstruction(Buf, EndBuf, Args, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000198
Misha Brukman12c29d12003-09-22 23:38:23 +0000199 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000200}
201
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000202
203/// ParseInstructionList - Parse all of the BasicBlock's & Instruction's in the
204/// body of a function. In post 1.0 bytecode files, we no longer emit basic
205/// block individually, in order to avoid per-basic-block overhead.
206unsigned BytecodeParser::ParseInstructionList(Function *F,
207 const unsigned char *&Buf,
208 const unsigned char *EndBuf) {
209 unsigned BlockNo = 0;
210 std::vector<unsigned> Args;
211
212 while (Buf < EndBuf) {
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];
220 ++BlockNo;
221 F->getBasicBlockList().push_back(BB);
222
223 // Read instructions into this basic block until we get to a terminator
224 while (Buf < EndBuf && !BB->getTerminator())
225 ParseInstruction(Buf, EndBuf, Args, BB);
226
227 if (!BB->getTerminator())
228 throw std::string("Non-terminated basic block found!");
229 }
230
231 return BlockNo;
232}
233
Misha Brukman12c29d12003-09-22 23:38:23 +0000234void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000235 const unsigned char *EndBuf,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000236 SymbolTable *ST,
237 Function *CurrentFunction) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000238 // Allow efficient basic block lookup by number.
239 std::vector<BasicBlock*> BBMap;
240 if (CurrentFunction)
241 for (Function::iterator I = CurrentFunction->begin(),
242 E = CurrentFunction->end(); I != E; ++I)
243 BBMap.push_back(I);
244
Chris Lattner00950542001-06-06 20:29:01 +0000245 while (Buf < EndBuf) {
246 // Symtab block header: [num entries][type id number]
247 unsigned NumEntries, Typ;
248 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000249 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000250 const Type *Ty = getType(Typ);
Chris Lattner927b1852003-10-09 20:22:47 +0000251 BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000252 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000253
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000254 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000255 // Symtab entry: [def slot #][name]
256 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000257 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000258 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000259 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000260 throw std::string("Failed reading symbol name.");
Chris Lattner00950542001-06-06 20:29:01 +0000261
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000262 Value *V = 0;
Chris Lattner36392bc2003-10-08 21:18:57 +0000263 if (Typ == Type::TypeTyID)
264 V = (Value*)getType(slot);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000265 else if (Typ == Type::LabelTyID) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000266 if (slot < BBMap.size())
267 V = BBMap[slot];
268 } else {
Chris Lattner36392bc2003-10-08 21:18:57 +0000269 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +0000270 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000271 if (V == 0) throw std::string("Failed value look-up.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000272 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000273 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000274
Chris Lattner52e20b02003-03-19 20:54:26 +0000275 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000276 }
277 }
278
Misha Brukman12c29d12003-09-22 23:38:23 +0000279 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000280}
281
Chris Lattner29b789b2003-11-19 17:27:18 +0000282void BytecodeParser::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
283 ConstantRefsType::iterator I =
284 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
285 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000286
Chris Lattner74734132002-08-17 22:01:27 +0000287 BCR_TRACE(3, "Mutating forward refs!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000288 Value *PH = I->second; // Get the placeholder...
289 PH->replaceAllUsesWith(NewV);
290 delete PH; // Delete the old placeholder
291 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000292}
293
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000294void BytecodeParser::ParseFunction(const unsigned char *&Buf,
295 const unsigned char *EndBuf) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000296 if (FunctionSignatureList.empty())
297 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000298
Chris Lattner29b789b2003-11-19 17:27:18 +0000299 Function *F = FunctionSignatureList.back();
Misha Brukman12c29d12003-09-22 23:38:23 +0000300 FunctionSignatureList.pop_back();
301
302 // Save the information for future reading of the function
Chris Lattner29b789b2003-11-19 17:27:18 +0000303 LazyFunctionLoadMap[F] = LazyFunctionInfo(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000304 // Pretend we've `parsed' this function
305 Buf = EndBuf;
306}
307
308void BytecodeParser::materializeFunction(Function* F) {
309 // Find {start, end} pointers and slot in the map. If not there, we're done.
Chris Lattner29b789b2003-11-19 17:27:18 +0000310 std::map<Function*, LazyFunctionInfo>::iterator Fi =
Misha Brukman12c29d12003-09-22 23:38:23 +0000311 LazyFunctionLoadMap.find(F);
312 if (Fi == LazyFunctionLoadMap.end()) return;
Chris Lattner29b789b2003-11-19 17:27:18 +0000313
314 const unsigned char *Buf = Fi->second.Buf;
315 const unsigned char *EndBuf = Fi->second.EndBuf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000316 LazyFunctionLoadMap.erase(Fi);
Chris Lattner00950542001-06-06 20:29:01 +0000317
Chris Lattnere3869c82003-04-16 21:16:05 +0000318 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
319
320 if (!hasInternalMarkerOnly) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000321 // We didn't support weak linkage explicitly.
Chris Lattnere3869c82003-04-16 21:16:05 +0000322 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000323 if (read_vbr(Buf, EndBuf, LinkageType))
324 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000325 if ((!hasExtendedLinkageSpecs && LinkageType > 3) ||
326 ( hasExtendedLinkageSpecs && LinkageType > 4))
Misha Brukman12c29d12003-09-22 23:38:23 +0000327 throw std::string("Invalid linkage type for Function.");
Chris Lattner6b252422003-10-16 18:28:50 +0000328 switch (LinkageType) {
329 case 0: Linkage = GlobalValue::ExternalLinkage; break;
330 case 1: Linkage = GlobalValue::WeakLinkage; break;
331 case 2: Linkage = GlobalValue::AppendingLinkage; break;
332 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000333 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000334 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000335 } else {
336 // We used to only support two linkage models: internal and external
337 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000338 if (read_vbr(Buf, EndBuf, isInternal))
339 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000340 if (isInternal) Linkage = GlobalValue::InternalLinkage;
341 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000342
Chris Lattnere3869c82003-04-16 21:16:05 +0000343 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000344
Chris Lattner52e20b02003-03-19 20:54:26 +0000345 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
346 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000347 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner927b1852003-10-09 20:22:47 +0000348 It != Params.end(); ++It, ++AI)
Chris Lattner29b789b2003-11-19 17:27:18 +0000349 insertValue(AI, getTypeSlot(AI->getType()), Values);
Chris Lattner00950542001-06-06 20:29:01 +0000350
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000351 // Keep track of how many basic blocks we have read in...
352 unsigned BlockNum = 0;
353
Chris Lattner00950542001-06-06 20:29:01 +0000354 while (Buf < EndBuf) {
355 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000356 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000357 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000358
359 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +0000360 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000361 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000362 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000363 break;
364
365 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000366 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000367 BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
368 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000369 break;
370 }
371
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000372 case BytecodeFormat::InstructionList: {
373 BCR_TRACE(2, "BLOCK BytecodeFormat::InstructionList: {\n");
374 if (BlockNum) throw std::string("Already parsed basic blocks!");
375 BlockNum = ParseInstructionList(F, Buf, Buf+Size);
376 break;
377 }
378
Chris Lattner29b789b2003-11-19 17:27:18 +0000379 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000380 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000381 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
Chris Lattner00950542001-06-06 20:29:01 +0000382 break;
383
384 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000385 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000386 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000387 if (OldBuf > Buf)
388 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000389 break;
390 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000391 BCR_TRACE(2, "} end block\n");
392
Misha Brukman12c29d12003-09-22 23:38:23 +0000393 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000394 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000395 }
396
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000397 // Make sure there were no references to non-existant basic blocks.
398 if (BlockNum != ParsedBasicBlocks.size())
399 throw std::string("Illegal basic block operand reference");
400 ParsedBasicBlocks.clear();
401
Chris Lattner29b789b2003-11-19 17:27:18 +0000402
Chris Lattner97330cf2003-10-09 23:10:14 +0000403 // Resolve forward references. Replace any uses of a forward reference value
404 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000405
Chris Lattner97330cf2003-10-09 23:10:14 +0000406 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
407 // number of operands. PHI nodes often have forward references, and can also
408 // often have a very large number of operands.
409 std::map<Value*, Value*> ForwardRefMapping;
410 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
411 I = ForwardReferences.begin(), E = ForwardReferences.end();
412 I != E; ++I)
413 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
414 false);
415
416 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
417 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
418 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
419 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
420 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
421 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
422 }
423
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000424 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +0000425 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
426 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000427 Value *PlaceHolder = I->second;
428 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +0000429
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000430 // Now that all the uses are gone, delete the placeholder...
431 // If we couldn't find a def (error case), then leak a little
432 // memory, because otherwise we can't remove all uses!
433 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +0000434 }
Chris Lattner00950542001-06-06 20:29:01 +0000435
Misha Brukman12c29d12003-09-22 23:38:23 +0000436 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000437 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000438
Chris Lattner52e20b02003-03-19 20:54:26 +0000439 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000440}
441
Misha Brukman12c29d12003-09-22 23:38:23 +0000442void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
443 const unsigned char *End) {
444 if (!FunctionSignatureList.empty())
445 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000446
Chris Lattner70cc3392001-09-10 07:58:01 +0000447 // Read global variables...
448 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000449 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000450 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000451 unsigned SlotNo;
452 GlobalValue::LinkageTypes Linkage;
453
454 if (!hasInternalMarkerOnly) {
Chris Lattner22482a12003-10-18 06:30:21 +0000455 unsigned LinkageID;
456 if (hasExtendedLinkageSpecs) {
457 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
458 // bit2,3,4 = Linkage, bit4+ = slot#
459 SlotNo = VarType >> 5;
460 LinkageID = (VarType >> 2) & 7;
461 } else {
462 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
463 // bit2,3 = Linkage, bit4+ = slot#
464 SlotNo = VarType >> 4;
465 LinkageID = (VarType >> 2) & 3;
466 }
467 switch (LinkageID) {
Chris Lattnerb91d9712003-10-18 19:48:10 +0000468 default: assert(0 && "Unknown linkage type!");
Chris Lattner6b252422003-10-16 18:28:50 +0000469 case 0: Linkage = GlobalValue::ExternalLinkage; break;
470 case 1: Linkage = GlobalValue::WeakLinkage; break;
471 case 2: Linkage = GlobalValue::AppendingLinkage; break;
472 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattner22482a12003-10-18 06:30:21 +0000473 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000474 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000475 } else {
476 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
477 // bit2 = isInternal, bit3+ = slot#
478 SlotNo = VarType >> 3;
479 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
480 GlobalValue::ExternalLinkage;
481 }
482
483 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000484 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000485 throw std::string("Global not pointer type! Ty = " +
486 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000487
Chris Lattner52e20b02003-03-19 20:54:26 +0000488 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000489
Chris Lattner70cc3392001-09-10 07:58:01 +0000490 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000491 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000492 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000493 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000494 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +0000495
Misha Brukman37f92e22003-09-11 22:34:13 +0000496 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000497 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000498 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000499 GlobalInits.push_back(std::make_pair(GV, InitSlot));
500 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000501 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000502 }
503
Chris Lattner52e20b02003-03-19 20:54:26 +0000504 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000505 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000506 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000507 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
508 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000509 if (!isa<PointerType>(Ty) ||
510 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000511 throw std::string("Function not ptr to func type! Ty = " +
512 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000513
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000514 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000515 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000516
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000517 // When the ModuleGlobalInfo section is read, we load the type of each
518 // function and the 'ModuleValues' slot that it lands in. We then load a
519 // placeholder into its slot to reserve it. When the function is loaded,
520 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000521
522 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000523 Function *Func = new Function(cast<FunctionType>(Ty),
524 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +0000525 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000526
Chris Lattner52e20b02003-03-19 20:54:26 +0000527 // Keep track of this information in a list that is emptied as functions are
528 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000529 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000530 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +0000531
Misha Brukman12c29d12003-09-22 23:38:23 +0000532 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000533 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000534 }
535
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000536 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000537
538 // Now that the function signature list is set up, reverse it so that we can
539 // remove elements efficiently from the back of the vector.
540 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000541
542 // This is for future proofing... in the future extra fields may be added that
543 // we don't understand, so we transparently ignore them.
544 //
545 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000546}
547
Misha Brukman12c29d12003-09-22 23:38:23 +0000548void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000549 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000550 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000551 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000552
553 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000554 Module::Endianness Endianness;
555 Module::PointerSize PointerSize;
556 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
557 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
558
559 bool hasNoEndianness = Version & 4;
560 bool hasNoPointerSize = Version & 8;
561
562 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000563
564 // Default values for the current bytecode version
Chris Lattnere3869c82003-04-16 21:16:05 +0000565 hasInternalMarkerOnly = false;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000566 hasExtendedLinkageSpecs = true;
567 hasOldStyleVarargs = false;
568 hasVarArgCallPadding = false;
Chris Lattnere3869c82003-04-16 21:16:05 +0000569 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000570
571 switch (RevisionNum) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000572 case 1: // LLVM pre-1.0 release: will be deleted on the next rev
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000573 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
574 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000575 hasInternalMarkerOnly = true;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000576 hasExtendedLinkageSpecs = false;
577 hasOldStyleVarargs = true;
578 hasVarArgCallPadding = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000579 break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000580 case 2: // LLVM pre-1.0 release:
Chris Lattnere3869c82003-04-16 21:16:05 +0000581 // Version #2 added information about all 4 linkage types instead of just
582 // having internal and external.
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000583 hasExtendedLinkageSpecs = false;
584 hasOldStyleVarargs = true;
585 hasVarArgCallPadding = true;
586 break;
587 case 0: // LLVM 1.0 release version
588 // Compared to rev #2, we added support for weak linkage, a more dense
589 // encoding, and better varargs support.
590
591 // FIXME: densify the encoding!
Chris Lattner036b8aa2003-03-06 17:55:45 +0000592 break;
593 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000594 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000595 }
596
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000597 if (hasNoEndianness) Endianness = Module::AnyEndianness;
598 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000599
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000600 TheModule->setEndianness(Endianness);
601 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000602 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000603 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
604 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000605}
606
Misha Brukman12c29d12003-09-22 23:38:23 +0000607void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000608 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000609 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000610 readBlock(Buf, EndBuf, Type, Size);
611 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
612 throw std::string("Expected Module packet! B: "+
613 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
614 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000615
Chris Lattner1d670cc2001-09-07 16:37:43 +0000616 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000617 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000618
619 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000620 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000621 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattner00950542001-06-06 20:29:01 +0000623 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000624 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000625 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000626 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000627 case BytecodeFormat::GlobalTypePlane:
628 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000629 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000630 break;
631
632 case BytecodeFormat::ModuleGlobalInfo:
633 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000634 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000635 break;
636
Chris Lattner1d670cc2001-09-07 16:37:43 +0000637 case BytecodeFormat::ConstantPool:
638 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000639 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000640 break;
641
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000642 case BytecodeFormat::Function: {
643 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000644 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000645 break;
646 }
647
648 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000649 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000650 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000651 break;
Chris Lattner00950542001-06-06 20:29:01 +0000652 default:
Chris Lattner00950542001-06-06 20:29:01 +0000653 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000654 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000655 break;
656 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000657 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000658 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000659 }
660
Chris Lattner52e20b02003-03-19 20:54:26 +0000661 // After the module constant pool has been read, we can safely initialize
662 // global variables...
663 while (!GlobalInits.empty()) {
664 GlobalVariable *GV = GlobalInits.back().first;
665 unsigned Slot = GlobalInits.back().second;
666 GlobalInits.pop_back();
667
668 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +0000669 // FIXME: Preserve this type ID!
670 unsigned TypeSlot = getTypeSlot(GV->getType()->getElementType());
671 if (Value *V = getValue(TypeSlot, Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000672 if (GV->hasInitializer())
673 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000674 GV->setInitializer(cast<Constant>(V));
675 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000676 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000677 }
678
Misha Brukman12c29d12003-09-22 23:38:23 +0000679 if (!FunctionSignatureList.empty())
680 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000681
682 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000683}
684
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000685void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
686 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000687
Misha Brukman12c29d12003-09-22 23:38:23 +0000688 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000689
Chris Lattner00950542001-06-06 20:29:01 +0000690 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000691 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000692 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000693 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
694 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000695
Chris Lattner75f20532003-04-22 18:02:52 +0000696 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000697 try {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000698 usesOldStyleVarargs = false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000699 ParseModule(Buf, EndBuf);
700 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000701 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000702 delete TheModule;
703 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000704 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000705 }
Chris Lattner00950542001-06-06 20:29:01 +0000706}