blob: 4c2ec041418fae5bac5c3eb4c135eb3d58cfa19d [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 Lattner74734132002-08-17 22:01:27 +000015// TODO: Return error messages to caller instead of printing them out directly.
Chris Lattner00950542001-06-06 20:29:01 +000016// TODO: Allow passing in an option to ignore the symbol table
17//
Chris Lattnerd6b65252001-10-24 01:15:12 +000018//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner7061dc52001-12-03 18:02:31 +000020#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/Bytecode/Reader.h"
22#include "llvm/Bytecode/Format.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000024#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/iOther.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000026#include "llvm/Module.h"
27#include "Support/StringExtras.h"
John Criswell7a73b802003-06-30 21:59:07 +000028#include "Config/unistd.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000029#include "Config/sys/mman.h"
30#include "Config/sys/stat.h"
31#include "Config/sys/types.h"
Chris Lattner00950542001-06-06 20:29:01 +000032#include <algorithm>
Misha Brukman12c29d12003-09-22 23:38:23 +000033#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000034
Brian Gaeked0fde302003-11-11 22:41:34 +000035namespace llvm {
36
Misha Brukmane0dd0d42003-09-23 16:15:29 +000037static inline void ALIGN32(const unsigned char *&begin,
38 const unsigned char *end) {
39 if (align32(begin, end))
40 throw std::string("Alignment error in buffer: read past end of block.");
41}
Misha Brukman12c29d12003-09-22 23:38:23 +000042
Chris Lattner9e460f22003-10-04 20:00:03 +000043unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
44 if (Ty->isPrimitiveType())
45 return Ty->getPrimitiveID();
46
47 // Check the function level types first...
48 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
49 FunctionTypeValues.end(), Ty);
50 if (I != FunctionTypeValues.end())
51 return FirstDerivedTyID + ModuleTypeValues.size() +
52 (&*I - &FunctionTypeValues[0]);
53
54 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
55 if (I == ModuleTypeValues.end())
56 throw std::string("Didn't find type in ModuleTypeValues.");
57 return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000058}
59
60const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner927b1852003-10-09 20:22:47 +000061 if (ID < Type::NumPrimitiveIDs)
62 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
63 return T;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner697954c2002-01-20 22:54:45 +000065 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner36392bc2003-10-08 21:18:57 +000066
Chris Lattner927b1852003-10-09 20:22:47 +000067 if (ID < Type::NumPrimitiveIDs)
68 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
69 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +000070
71 // Otherwise, derived types need offset...
72 ID -= FirstDerivedTyID;
73
74 // Is it a module-level type?
75 if (ID < ModuleTypeValues.size())
76 return ModuleTypeValues[ID].get();
77
78 // Nope, is it a function-level type?
79 ID -= ModuleTypeValues.size();
80 if (ID < FunctionTypeValues.size())
81 return FunctionTypeValues[ID].get();
82
Chris Lattner927b1852003-10-09 20:22:47 +000083 throw std::string("Illegal type reference!");
Chris Lattner00950542001-06-06 20:29:01 +000084}
85
Chris Lattner927b1852003-10-09 20:22:47 +000086unsigned BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
Chris Lattnerf0d92732003-10-13 14:34:59 +000087 return insertValue(Val, getTypeSlot(Val->getType()), ValueTab);
88}
89
90unsigned BytecodeParser::insertValue(Value *Val, unsigned type,
91 ValueTable &ValueTab) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +000092 assert((!isa<Constant>(Val) || Val->getType()->isPrimitiveType() ||
Chris Lattner52e20b02003-03-19 20:54:26 +000093 !cast<Constant>(Val)->isNullValue()) &&
94 "Cannot read null values from bytecode!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000095 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000096
Chris Lattner52e20b02003-03-19 20:54:26 +000097 if (ValueTab.size() <= type) {
98 unsigned OldSize = ValueTab.size();
99 ValueTab.resize(type+1);
100 while (OldSize != type+1)
101 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +0000102 }
Chris Lattner00950542001-06-06 20:29:01 +0000103
104 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +0000105 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +0000106 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +0000107
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000108 bool HasOffset = !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +0000109 return ValueTab[type]->size()-1 + HasOffset;
110}
111
112
Chris Lattner00950542001-06-06 20:29:01 +0000113Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
Chris Lattner36392bc2003-10-08 21:18:57 +0000114 return getValue(getTypeSlot(Ty), oNum, Create);
115}
116
117Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
118 assert(type != Type::TypeTyID && "getValue() cannot get types!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000119 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000120 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000121
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000122 if (type >= FirstDerivedTyID) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000123 if (Num == 0)
Chris Lattner36392bc2003-10-08 21:18:57 +0000124 return Constant::getNullValue(getType(type));
Chris Lattner52e20b02003-03-19 20:54:26 +0000125 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
127
Chris Lattner52e20b02003-03-19 20:54:26 +0000128 if (type < ModuleValues.size()) {
129 if (Num < ModuleValues[type]->size())
130 return ModuleValues[type]->getOperand(Num);
131 Num -= ModuleValues[type]->size();
132 }
133
134 if (Values.size() > type && Values[type]->size() > Num)
135 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner74734132002-08-17 22:01:27 +0000137 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000139 std::pair<unsigned,unsigned> KeyValue(type, oNum);
140 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
141 ForwardReferences.lower_bound(KeyValue);
142 if (I != ForwardReferences.end() && I->first == KeyValue)
143 return I->second; // We have already created this placeholder
144
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000145 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000146 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000147 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000148}
149
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000150/// getBasicBlock - Get a particular numbered basic block, which might be a
151/// forward reference. This works together with ParseBasicBlock to handle these
152/// forward references in a clean manner.
153///
154BasicBlock *BytecodeParser::getBasicBlock(unsigned ID) {
155 // Make sure there is room in the table...
156 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
157
158 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
159 // has already created this block, or if the forward reference has already
160 // been created.
161 if (ParsedBasicBlocks[ID])
162 return ParsedBasicBlocks[ID];
163
164 // Otherwise, the basic block has not yet been created. Do so and add it to
165 // the ParsedBasicBlocks list.
166 return ParsedBasicBlocks[ID] = new BasicBlock();
167}
168
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000169/// getConstantValue - Just like getValue, except that it returns a null pointer
170/// only on error. It always returns a constant (meaning that if the value is
171/// defined, but is not a constant, that is an error). If the specified
172/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
173/// after the real value is parsed, the placeholder is eliminated.
174///
Chris Lattner1c3673b2003-11-19 06:01:12 +0000175Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
176 if (Value *V = getValue(TypeSlot, Slot, false))
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000177 if (Constant *C = dyn_cast<Constant>(V))
178 return C; // If we already have the value parsed, just return it
179 else
180 throw std::string("Reference of a value is expected to be a constant!");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000181
Chris Lattner1c3673b2003-11-19 06:01:12 +0000182 const Type *Ty = getType(TypeSlot);
Chris Lattner52e20b02003-03-19 20:54:26 +0000183 std::pair<const Type*, unsigned> Key(Ty, Slot);
184 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
185
186 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000187 BCR_TRACE(5, "Previous forward ref found!\n");
188 return cast<Constant>(I->second);
189 } else {
190 // Create a placeholder for the constant reference and
191 // keep track of the fact that we have a forward ref to recycle it
192 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
193 Constant *C = new ConstPHolder(Ty, Slot);
194
195 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner52e20b02003-03-19 20:54:26 +0000196 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000197 return C;
198 }
199}
200
201
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000202BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
203 const unsigned char *EndBuf,
204 unsigned BlockNo) {
205 BasicBlock *BB;
206 if (ParsedBasicBlocks.size() == BlockNo)
207 ParsedBasicBlocks.push_back(BB = new BasicBlock());
208 else if (ParsedBasicBlocks[BlockNo] == 0)
209 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
210 else
211 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000212
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000213 std::vector<unsigned> Args;
214 while (Buf < EndBuf)
215 ParseInstruction(Buf, EndBuf, Args, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000216
Misha Brukman12c29d12003-09-22 23:38:23 +0000217 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000218}
219
Misha Brukman12c29d12003-09-22 23:38:23 +0000220void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000221 const unsigned char *EndBuf,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000222 SymbolTable *ST,
223 Function *CurrentFunction) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000224 // Allow efficient basic block lookup by number.
225 std::vector<BasicBlock*> BBMap;
226 if (CurrentFunction)
227 for (Function::iterator I = CurrentFunction->begin(),
228 E = CurrentFunction->end(); I != E; ++I)
229 BBMap.push_back(I);
230
Chris Lattner00950542001-06-06 20:29:01 +0000231 while (Buf < EndBuf) {
232 // Symtab block header: [num entries][type id number]
233 unsigned NumEntries, Typ;
234 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000235 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000236 const Type *Ty = getType(Typ);
Chris Lattner927b1852003-10-09 20:22:47 +0000237 BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000238 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000239
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000240 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000241 // Symtab entry: [def slot #][name]
242 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000243 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000244 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000245 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000246 throw std::string("Failed reading symbol name.");
Chris Lattner00950542001-06-06 20:29:01 +0000247
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000248 Value *V = 0;
Chris Lattner36392bc2003-10-08 21:18:57 +0000249 if (Typ == Type::TypeTyID)
250 V = (Value*)getType(slot);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000251 else if (Typ == Type::LabelTyID) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000252 if (slot < BBMap.size())
253 V = BBMap[slot];
254 } else {
Chris Lattner36392bc2003-10-08 21:18:57 +0000255 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +0000256 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000257 if (V == 0) throw std::string("Failed value look-up.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000258 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000259 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000260
Chris Lattner52e20b02003-03-19 20:54:26 +0000261 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
263 }
264
Misha Brukman12c29d12003-09-22 23:38:23 +0000265 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000266}
267
Chris Lattner74734132002-08-17 22:01:27 +0000268void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000269 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
270 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000271 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000272
Chris Lattner74734132002-08-17 22:01:27 +0000273 BCR_TRACE(3, "Mutating forward refs!\n");
274 Value *VPH = I->second; // Get the placeholder...
Chris Lattner52e20b02003-03-19 20:54:26 +0000275 VPH->replaceAllUsesWith(NewV);
276
277 // If this is a global variable being resolved, remove the placeholder from
278 // the module...
279 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
280 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000281
Chris Lattner74734132002-08-17 22:01:27 +0000282 delete VPH; // Delete the old placeholder
283 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000284}
285
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000286void BytecodeParser::ParseFunction(const unsigned char *&Buf,
287 const unsigned char *EndBuf) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000288 if (FunctionSignatureList.empty())
289 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000290
Misha Brukman12c29d12003-09-22 23:38:23 +0000291 Function *F = FunctionSignatureList.back().first;
292 unsigned FunctionSlot = FunctionSignatureList.back().second;
293 FunctionSignatureList.pop_back();
294
295 // Save the information for future reading of the function
296 LazyFunctionInfo *LFI = new LazyFunctionInfo();
297 LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
298 LazyFunctionLoadMap[F] = LFI;
299 // Pretend we've `parsed' this function
300 Buf = EndBuf;
301}
302
303void BytecodeParser::materializeFunction(Function* F) {
304 // Find {start, end} pointers and slot in the map. If not there, we're done.
305 std::map<Function*, LazyFunctionInfo*>::iterator Fi =
306 LazyFunctionLoadMap.find(F);
307 if (Fi == LazyFunctionLoadMap.end()) return;
308
309 LazyFunctionInfo *LFI = Fi->second;
310 const unsigned char *Buf = LFI->Buf;
311 const unsigned char *EndBuf = LFI->EndBuf;
312 unsigned FunctionSlot = LFI->FunctionSlot;
313 LazyFunctionLoadMap.erase(Fi);
314 delete LFI;
Chris Lattner00950542001-06-06 20:29:01 +0000315
Chris Lattnere3869c82003-04-16 21:16:05 +0000316 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
317
318 if (!hasInternalMarkerOnly) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000319 // We didn't support weak linkage explicitly.
Chris Lattnere3869c82003-04-16 21:16:05 +0000320 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000321 if (read_vbr(Buf, EndBuf, LinkageType))
322 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000323 if ((!hasExtendedLinkageSpecs && LinkageType > 3) ||
324 ( hasExtendedLinkageSpecs && LinkageType > 4))
Misha Brukman12c29d12003-09-22 23:38:23 +0000325 throw std::string("Invalid linkage type for Function.");
Chris Lattner6b252422003-10-16 18:28:50 +0000326 switch (LinkageType) {
327 case 0: Linkage = GlobalValue::ExternalLinkage; break;
328 case 1: Linkage = GlobalValue::WeakLinkage; break;
329 case 2: Linkage = GlobalValue::AppendingLinkage; break;
330 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000331 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000332 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000333 } else {
334 // We used to only support two linkage models: internal and external
335 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000336 if (read_vbr(Buf, EndBuf, isInternal))
337 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000338 if (isInternal) Linkage = GlobalValue::InternalLinkage;
339 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000340
Chris Lattnere3869c82003-04-16 21:16:05 +0000341 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000342
Chris Lattner52e20b02003-03-19 20:54:26 +0000343 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
344 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000345 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner927b1852003-10-09 20:22:47 +0000346 It != Params.end(); ++It, ++AI)
347 insertValue(AI, Values);
Chris Lattner00950542001-06-06 20:29:01 +0000348
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000349 // Keep track of how many basic blocks we have read in...
350 unsigned BlockNum = 0;
351
Chris Lattner00950542001-06-06 20:29:01 +0000352 while (Buf < EndBuf) {
353 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000354 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000355 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000356
357 switch (Type) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000358 case BytecodeFormat::ConstantPool: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000359 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000360 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000361 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000362 }
Chris Lattner00950542001-06-06 20:29:01 +0000363
364 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000365 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000366 BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
367 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000368 break;
369 }
370
Misha Brukman12c29d12003-09-22 23:38:23 +0000371 case BytecodeFormat::SymbolTable: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000372 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000373 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
Chris Lattner00950542001-06-06 20:29:01 +0000374 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000375 }
Chris Lattner00950542001-06-06 20:29:01 +0000376
377 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000378 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000379 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000380 if (OldBuf > Buf)
381 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000382 break;
383 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000384 BCR_TRACE(2, "} end block\n");
385
Misha Brukman12c29d12003-09-22 23:38:23 +0000386 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000387 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000388 }
389
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000390 // Make sure there were no references to non-existant basic blocks.
391 if (BlockNum != ParsedBasicBlocks.size())
392 throw std::string("Illegal basic block operand reference");
393 ParsedBasicBlocks.clear();
394
Chris Lattner97330cf2003-10-09 23:10:14 +0000395 // Resolve forward references. Replace any uses of a forward reference value
396 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000397
Chris Lattner97330cf2003-10-09 23:10:14 +0000398 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
399 // number of operands. PHI nodes often have forward references, and can also
400 // often have a very large number of operands.
401 std::map<Value*, Value*> ForwardRefMapping;
402 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
403 I = ForwardReferences.begin(), E = ForwardReferences.end();
404 I != E; ++I)
405 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
406 false);
407
408 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
409 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
410 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
411 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
412 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
413 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
414 }
415
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000416 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +0000417 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
418 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000419 Value *PlaceHolder = I->second;
420 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +0000421
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000422 // Now that all the uses are gone, delete the placeholder...
423 // If we couldn't find a def (error case), then leak a little
424 // memory, because otherwise we can't remove all uses!
425 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +0000426 }
Chris Lattner00950542001-06-06 20:29:01 +0000427
Misha Brukman12c29d12003-09-22 23:38:23 +0000428 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000429 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000430
Chris Lattner52e20b02003-03-19 20:54:26 +0000431 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000432}
433
Misha Brukman12c29d12003-09-22 23:38:23 +0000434void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
435 const unsigned char *End) {
436 if (!FunctionSignatureList.empty())
437 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000438
Chris Lattner70cc3392001-09-10 07:58:01 +0000439 // Read global variables...
440 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000441 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000442 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000443 unsigned SlotNo;
444 GlobalValue::LinkageTypes Linkage;
445
446 if (!hasInternalMarkerOnly) {
Chris Lattner22482a12003-10-18 06:30:21 +0000447 unsigned LinkageID;
448 if (hasExtendedLinkageSpecs) {
449 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
450 // bit2,3,4 = Linkage, bit4+ = slot#
451 SlotNo = VarType >> 5;
452 LinkageID = (VarType >> 2) & 7;
453 } else {
454 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
455 // bit2,3 = Linkage, bit4+ = slot#
456 SlotNo = VarType >> 4;
457 LinkageID = (VarType >> 2) & 3;
458 }
459 switch (LinkageID) {
Chris Lattnerb91d9712003-10-18 19:48:10 +0000460 default: assert(0 && "Unknown linkage type!");
Chris Lattner6b252422003-10-16 18:28:50 +0000461 case 0: Linkage = GlobalValue::ExternalLinkage; break;
462 case 1: Linkage = GlobalValue::WeakLinkage; break;
463 case 2: Linkage = GlobalValue::AppendingLinkage; break;
464 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattner22482a12003-10-18 06:30:21 +0000465 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000466 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000467 } else {
468 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
469 // bit2 = isInternal, bit3+ = slot#
470 SlotNo = VarType >> 3;
471 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
472 GlobalValue::ExternalLinkage;
473 }
474
475 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000476 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000477 throw std::string("Global not pointer type! Ty = " +
478 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000479
Chris Lattner52e20b02003-03-19 20:54:26 +0000480 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000481
Chris Lattner70cc3392001-09-10 07:58:01 +0000482 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000483 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000484 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000485 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000486 ResolveReferencesToValue(GV, insertValue(GV, SlotNo, ModuleValues));
Chris Lattner05950c32001-10-13 06:47:01 +0000487
Misha Brukman37f92e22003-09-11 22:34:13 +0000488 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000489 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000490 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000491 GlobalInits.push_back(std::make_pair(GV, InitSlot));
492 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000493 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000494 }
495
Chris Lattner52e20b02003-03-19 20:54:26 +0000496 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000497 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000498 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000499 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
500 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000501 if (!isa<PointerType>(Ty) ||
502 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000503 throw std::string("Function not ptr to func type! Ty = " +
504 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000505
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000506 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000507 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000508
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000509 // When the ModuleGlobalInfo section is read, we load the type of each
510 // function and the 'ModuleValues' slot that it lands in. We then load a
511 // placeholder into its slot to reserve it. When the function is loaded,
512 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000513
514 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000515 Function *Func = new Function(cast<FunctionType>(Ty),
516 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattnerf0d92732003-10-13 14:34:59 +0000517 unsigned DestSlot = insertValue(Func, FnSignature, ModuleValues);
Chris Lattner927b1852003-10-09 20:22:47 +0000518 ResolveReferencesToValue(Func, DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000519
Chris Lattner52e20b02003-03-19 20:54:26 +0000520 // Keep track of this information in a list that is emptied as functions are
521 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000522 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000523 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
524
Misha Brukman12c29d12003-09-22 23:38:23 +0000525 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000526 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000527 }
528
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000529 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000530
531 // Now that the function signature list is set up, reverse it so that we can
532 // remove elements efficiently from the back of the vector.
533 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000534
535 // This is for future proofing... in the future extra fields may be added that
536 // we don't understand, so we transparently ignore them.
537 //
538 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000539}
540
Misha Brukman12c29d12003-09-22 23:38:23 +0000541void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000542 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000543 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000544 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000545
546 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000547 Module::Endianness Endianness;
548 Module::PointerSize PointerSize;
549 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
550 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
551
552 bool hasNoEndianness = Version & 4;
553 bool hasNoPointerSize = Version & 8;
554
555 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000556
557 // Default values for the current bytecode version
Chris Lattnere3869c82003-04-16 21:16:05 +0000558 hasInternalMarkerOnly = false;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000559 hasExtendedLinkageSpecs = true;
560 hasOldStyleVarargs = false;
561 hasVarArgCallPadding = false;
Chris Lattnere3869c82003-04-16 21:16:05 +0000562 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000563
564 switch (RevisionNum) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000565 case 1: // LLVM pre-1.0 release: will be deleted on the next rev
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000566 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
567 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000568 hasInternalMarkerOnly = true;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000569 hasExtendedLinkageSpecs = false;
570 hasOldStyleVarargs = true;
571 hasVarArgCallPadding = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000572 break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000573 case 2: // LLVM pre-1.0 release:
Chris Lattnere3869c82003-04-16 21:16:05 +0000574 // Version #2 added information about all 4 linkage types instead of just
575 // having internal and external.
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000576 hasExtendedLinkageSpecs = false;
577 hasOldStyleVarargs = true;
578 hasVarArgCallPadding = true;
579 break;
580 case 0: // LLVM 1.0 release version
581 // Compared to rev #2, we added support for weak linkage, a more dense
582 // encoding, and better varargs support.
583
584 // FIXME: densify the encoding!
Chris Lattner036b8aa2003-03-06 17:55:45 +0000585 break;
586 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000587 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000588 }
589
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000590 if (hasNoEndianness) Endianness = Module::AnyEndianness;
591 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000592
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000593 TheModule->setEndianness(Endianness);
594 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000595 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000596 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
597 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000598}
599
Misha Brukman12c29d12003-09-22 23:38:23 +0000600void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000601 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000602 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000603 readBlock(Buf, EndBuf, Type, Size);
604 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
605 throw std::string("Expected Module packet! B: "+
606 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
607 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000608
Chris Lattner1d670cc2001-09-07 16:37:43 +0000609 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000610 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000611
612 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000613 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000614 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000615
Chris Lattner00950542001-06-06 20:29:01 +0000616 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000617 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000618 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000619 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000620 case BytecodeFormat::GlobalTypePlane:
621 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000622 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000623 break;
624
625 case BytecodeFormat::ModuleGlobalInfo:
626 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000627 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000628 break;
629
Chris Lattner1d670cc2001-09-07 16:37:43 +0000630 case BytecodeFormat::ConstantPool:
631 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000632 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000633 break;
634
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000635 case BytecodeFormat::Function: {
636 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000637 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000638 break;
639 }
640
641 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000642 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000643 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000644 break;
645
646 default:
Chris Lattner00950542001-06-06 20:29:01 +0000647 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000648 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000649 break;
650 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000651 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000652 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000653 }
654
Chris Lattner52e20b02003-03-19 20:54:26 +0000655 // After the module constant pool has been read, we can safely initialize
656 // global variables...
657 while (!GlobalInits.empty()) {
658 GlobalVariable *GV = GlobalInits.back().first;
659 unsigned Slot = GlobalInits.back().second;
660 GlobalInits.pop_back();
661
662 // Look up the initializer value...
663 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000664 if (GV->hasInitializer())
665 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000666 GV->setInitializer(cast<Constant>(V));
667 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000668 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000669 }
670
Misha Brukman12c29d12003-09-22 23:38:23 +0000671 if (!FunctionSignatureList.empty())
672 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000673
674 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000675}
676
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000677void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
678 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000679
Misha Brukman12c29d12003-09-22 23:38:23 +0000680 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000681
Chris Lattner00950542001-06-06 20:29:01 +0000682 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000683 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000684 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000685 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
686 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000687
Chris Lattner75f20532003-04-22 18:02:52 +0000688 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000689 try {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000690 usesOldStyleVarargs = false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000691 ParseModule(Buf, EndBuf);
692 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000693 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000694 delete TheModule;
695 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000696 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000697 }
Chris Lattner00950542001-06-06 20:29:01 +0000698}
Brian Gaeked0fde302003-11-11 22:41:34 +0000699
700} // End llvm namespace