blob: 097d548b98e1e819b1f56120b97167dae784a77e [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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000023#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/iOther.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000025#include "llvm/Module.h"
26#include "Support/StringExtras.h"
John Criswell7a73b802003-06-30 21:59:07 +000027#include "Config/unistd.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000028#include "Config/sys/mman.h"
29#include "Config/sys/stat.h"
30#include "Config/sys/types.h"
Chris Lattner00950542001-06-06 20:29:01 +000031#include <algorithm>
Misha Brukman12c29d12003-09-22 23:38:23 +000032#include <memory>
Chris Lattner29b789b2003-11-19 17:27:18 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Misha Brukmane0dd0d42003-09-23 16:15:29 +000035static inline void ALIGN32(const unsigned char *&begin,
36 const unsigned char *end) {
37 if (align32(begin, end))
38 throw std::string("Alignment error in buffer: read past end of block.");
39}
Misha Brukman12c29d12003-09-22 23:38:23 +000040
Chris Lattner9e460f22003-10-04 20:00:03 +000041unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
42 if (Ty->isPrimitiveType())
43 return Ty->getPrimitiveID();
44
45 // Check the function level types first...
46 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
47 FunctionTypeValues.end(), Ty);
48 if (I != FunctionTypeValues.end())
49 return FirstDerivedTyID + ModuleTypeValues.size() +
50 (&*I - &FunctionTypeValues[0]);
51
52 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
53 if (I == ModuleTypeValues.end())
54 throw std::string("Didn't find type in ModuleTypeValues.");
55 return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
58const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner927b1852003-10-09 20:22:47 +000059 if (ID < Type::NumPrimitiveIDs)
60 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
61 return T;
Chris Lattner00950542001-06-06 20:29:01 +000062
Chris Lattner697954c2002-01-20 22:54:45 +000063 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner36392bc2003-10-08 21:18:57 +000064
Chris Lattner927b1852003-10-09 20:22:47 +000065 if (ID < Type::NumPrimitiveIDs)
66 if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
67 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +000068
69 // Otherwise, derived types need offset...
70 ID -= FirstDerivedTyID;
71
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 Lattnerf0d92732003-10-13 14:34:59 +000084unsigned BytecodeParser::insertValue(Value *Val, unsigned type,
85 ValueTable &ValueTab) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +000086 assert((!isa<Constant>(Val) || Val->getType()->isPrimitiveType() ||
Chris Lattner52e20b02003-03-19 20:54:26 +000087 !cast<Constant>(Val)->isNullValue()) &&
88 "Cannot read null values from bytecode!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000089 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner29b789b2003-11-19 17:27:18 +000090
Chris Lattner52e20b02003-03-19 20:54:26 +000091 if (ValueTab.size() <= type) {
92 unsigned OldSize = ValueTab.size();
93 ValueTab.resize(type+1);
Chris Lattner036b8aa2003-03-06 17:55:45 +000094 }
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattner29b789b2003-11-19 17:27:18 +000096 if (!ValueTab[type]) ValueTab[type] = new ValueList();
97
Chris Lattner00950542001-06-06 20:29:01 +000098 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000099 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +0000100 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +0000101
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000102 bool HasOffset = !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +0000103 return ValueTab[type]->size()-1 + HasOffset;
104}
105
106
Chris Lattner36392bc2003-10-08 21:18:57 +0000107Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
108 assert(type != Type::TypeTyID && "getValue() cannot get types!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000109 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000110 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000112 if (type >= FirstDerivedTyID) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000113 if (Num == 0)
Chris Lattner36392bc2003-10-08 21:18:57 +0000114 return Constant::getNullValue(getType(type));
Chris Lattner52e20b02003-03-19 20:54:26 +0000115 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000116 }
117
Chris Lattner29b789b2003-11-19 17:27:18 +0000118 if (type < ModuleValues.size() && ModuleValues[type]) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000119 if (Num < ModuleValues[type]->size())
120 return ModuleValues[type]->getOperand(Num);
121 Num -= ModuleValues[type]->size();
122 }
123
Chris Lattner29b789b2003-11-19 17:27:18 +0000124 if (Values.size() > type && Values[type] && Num < Values[type]->size())
Chris Lattner52e20b02003-03-19 20:54:26 +0000125 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000126
Chris Lattner74734132002-08-17 22:01:27 +0000127 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000129 std::pair<unsigned,unsigned> KeyValue(type, oNum);
130 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
131 ForwardReferences.lower_bound(KeyValue);
132 if (I != ForwardReferences.end() && I->first == KeyValue)
133 return I->second; // We have already created this placeholder
134
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000135 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000136 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000137 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000138}
139
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000140/// getBasicBlock - Get a particular numbered basic block, which might be a
141/// forward reference. This works together with ParseBasicBlock to handle these
142/// forward references in a clean manner.
143///
144BasicBlock *BytecodeParser::getBasicBlock(unsigned ID) {
145 // Make sure there is room in the table...
146 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
147
148 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
149 // has already created this block, or if the forward reference has already
150 // been created.
151 if (ParsedBasicBlocks[ID])
152 return ParsedBasicBlocks[ID];
153
154 // Otherwise, the basic block has not yet been created. Do so and add it to
155 // the ParsedBasicBlocks list.
156 return ParsedBasicBlocks[ID] = new BasicBlock();
157}
158
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000159/// getConstantValue - Just like getValue, except that it returns a null pointer
160/// only on error. It always returns a constant (meaning that if the value is
161/// defined, but is not a constant, that is an error). If the specified
162/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
163/// after the real value is parsed, the placeholder is eliminated.
164///
Chris Lattner1c3673b2003-11-19 06:01:12 +0000165Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
166 if (Value *V = getValue(TypeSlot, Slot, false))
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000167 if (Constant *C = dyn_cast<Constant>(V))
168 return C; // If we already have the value parsed, just return it
169 else
170 throw std::string("Reference of a value is expected to be a constant!");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000171
Chris Lattner1c3673b2003-11-19 06:01:12 +0000172 const Type *Ty = getType(TypeSlot);
Chris Lattner52e20b02003-03-19 20:54:26 +0000173 std::pair<const Type*, unsigned> Key(Ty, Slot);
Chris Lattner29b789b2003-11-19 17:27:18 +0000174 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
Chris Lattner52e20b02003-03-19 20:54:26 +0000175
Chris Lattner29b789b2003-11-19 17:27:18 +0000176 if (I != ConstantFwdRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000177 BCR_TRACE(5, "Previous forward ref found!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000178 return I->second;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000179 } else {
180 // Create a placeholder for the constant reference and
181 // keep track of the fact that we have a forward ref to recycle it
182 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
183 Constant *C = new ConstPHolder(Ty, Slot);
184
185 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner29b789b2003-11-19 17:27:18 +0000186 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000187 return C;
188 }
189}
190
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000191/// ParseBasicBlock - In LLVM 1.0 bytecode files, we used to output one
192/// basicblock at a time. This method reads in one of the basicblock packets.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000193BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
194 const unsigned char *EndBuf,
195 unsigned BlockNo) {
196 BasicBlock *BB;
197 if (ParsedBasicBlocks.size() == BlockNo)
198 ParsedBasicBlocks.push_back(BB = new BasicBlock());
199 else if (ParsedBasicBlocks[BlockNo] == 0)
200 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
201 else
202 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000203
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000204 std::vector<unsigned> Args;
205 while (Buf < EndBuf)
206 ParseInstruction(Buf, EndBuf, Args, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000207
Misha Brukman12c29d12003-09-22 23:38:23 +0000208 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000209}
210
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000211
212/// ParseInstructionList - Parse all of the BasicBlock's & Instruction's in the
213/// body of a function. In post 1.0 bytecode files, we no longer emit basic
214/// block individually, in order to avoid per-basic-block overhead.
215unsigned BytecodeParser::ParseInstructionList(Function *F,
216 const unsigned char *&Buf,
217 const unsigned char *EndBuf) {
218 unsigned BlockNo = 0;
219 std::vector<unsigned> Args;
220
221 while (Buf < EndBuf) {
222 BasicBlock *BB;
223 if (ParsedBasicBlocks.size() == BlockNo)
224 ParsedBasicBlocks.push_back(BB = new BasicBlock());
225 else if (ParsedBasicBlocks[BlockNo] == 0)
226 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
227 else
228 BB = ParsedBasicBlocks[BlockNo];
229 ++BlockNo;
230 F->getBasicBlockList().push_back(BB);
231
232 // Read instructions into this basic block until we get to a terminator
233 while (Buf < EndBuf && !BB->getTerminator())
234 ParseInstruction(Buf, EndBuf, Args, BB);
235
236 if (!BB->getTerminator())
237 throw std::string("Non-terminated basic block found!");
238 }
239
240 return BlockNo;
241}
242
Misha Brukman12c29d12003-09-22 23:38:23 +0000243void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000244 const unsigned char *EndBuf,
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000245 SymbolTable *ST,
246 Function *CurrentFunction) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000247 // Allow efficient basic block lookup by number.
248 std::vector<BasicBlock*> BBMap;
249 if (CurrentFunction)
250 for (Function::iterator I = CurrentFunction->begin(),
251 E = CurrentFunction->end(); I != E; ++I)
252 BBMap.push_back(I);
253
Chris Lattner00950542001-06-06 20:29:01 +0000254 while (Buf < EndBuf) {
255 // Symtab block header: [num entries][type id number]
256 unsigned NumEntries, Typ;
257 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000258 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000259 const Type *Ty = getType(Typ);
Chris Lattner927b1852003-10-09 20:22:47 +0000260 BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000261 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000262
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000263 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000264 // Symtab entry: [def slot #][name]
265 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000266 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000267 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000268 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000269 throw std::string("Failed reading symbol name.");
Chris Lattner00950542001-06-06 20:29:01 +0000270
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000271 Value *V = 0;
Chris Lattner36392bc2003-10-08 21:18:57 +0000272 if (Typ == Type::TypeTyID)
273 V = (Value*)getType(slot);
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000274 else if (Typ == Type::LabelTyID) {
Chris Lattner39cacce2003-10-10 05:43:47 +0000275 if (slot < BBMap.size())
276 V = BBMap[slot];
277 } else {
Chris Lattner36392bc2003-10-08 21:18:57 +0000278 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +0000279 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000280 if (V == 0) throw std::string("Failed value look-up.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000281 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000282 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000283
Chris Lattner52e20b02003-03-19 20:54:26 +0000284 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000285 }
286 }
287
Misha Brukman12c29d12003-09-22 23:38:23 +0000288 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000289}
290
Chris Lattner29b789b2003-11-19 17:27:18 +0000291void BytecodeParser::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
292 ConstantRefsType::iterator I =
293 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
294 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000295
Chris Lattner74734132002-08-17 22:01:27 +0000296 BCR_TRACE(3, "Mutating forward refs!\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000297 Value *PH = I->second; // Get the placeholder...
298 PH->replaceAllUsesWith(NewV);
299 delete PH; // Delete the old placeholder
300 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000301}
302
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000303void BytecodeParser::ParseFunction(const unsigned char *&Buf,
304 const unsigned char *EndBuf) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000305 if (FunctionSignatureList.empty())
306 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000307
Chris Lattner29b789b2003-11-19 17:27:18 +0000308 Function *F = FunctionSignatureList.back();
Misha Brukman12c29d12003-09-22 23:38:23 +0000309 FunctionSignatureList.pop_back();
310
311 // Save the information for future reading of the function
Chris Lattner29b789b2003-11-19 17:27:18 +0000312 LazyFunctionLoadMap[F] = LazyFunctionInfo(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000313 // Pretend we've `parsed' this function
314 Buf = EndBuf;
315}
316
317void BytecodeParser::materializeFunction(Function* F) {
318 // Find {start, end} pointers and slot in the map. If not there, we're done.
Chris Lattner29b789b2003-11-19 17:27:18 +0000319 std::map<Function*, LazyFunctionInfo>::iterator Fi =
Misha Brukman12c29d12003-09-22 23:38:23 +0000320 LazyFunctionLoadMap.find(F);
321 if (Fi == LazyFunctionLoadMap.end()) return;
Chris Lattner29b789b2003-11-19 17:27:18 +0000322
323 const unsigned char *Buf = Fi->second.Buf;
324 const unsigned char *EndBuf = Fi->second.EndBuf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000325 LazyFunctionLoadMap.erase(Fi);
Chris Lattner00950542001-06-06 20:29:01 +0000326
Chris Lattnere3869c82003-04-16 21:16:05 +0000327 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
328
329 if (!hasInternalMarkerOnly) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000330 // We didn't support weak linkage explicitly.
Chris Lattnere3869c82003-04-16 21:16:05 +0000331 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000332 if (read_vbr(Buf, EndBuf, LinkageType))
333 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000334 if ((!hasExtendedLinkageSpecs && LinkageType > 3) ||
335 ( hasExtendedLinkageSpecs && LinkageType > 4))
Misha Brukman12c29d12003-09-22 23:38:23 +0000336 throw std::string("Invalid linkage type for Function.");
Chris Lattner6b252422003-10-16 18:28:50 +0000337 switch (LinkageType) {
338 case 0: Linkage = GlobalValue::ExternalLinkage; break;
339 case 1: Linkage = GlobalValue::WeakLinkage; break;
340 case 2: Linkage = GlobalValue::AppendingLinkage; break;
341 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000342 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000343 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000344 } else {
345 // We used to only support two linkage models: internal and external
346 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000347 if (read_vbr(Buf, EndBuf, isInternal))
348 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000349 if (isInternal) Linkage = GlobalValue::InternalLinkage;
350 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000351
Chris Lattnere3869c82003-04-16 21:16:05 +0000352 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000353
Chris Lattner52e20b02003-03-19 20:54:26 +0000354 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
355 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000356 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner927b1852003-10-09 20:22:47 +0000357 It != Params.end(); ++It, ++AI)
Chris Lattner29b789b2003-11-19 17:27:18 +0000358 insertValue(AI, getTypeSlot(AI->getType()), Values);
Chris Lattner00950542001-06-06 20:29:01 +0000359
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000360 // Keep track of how many basic blocks we have read in...
361 unsigned BlockNum = 0;
362
Chris Lattner00950542001-06-06 20:29:01 +0000363 while (Buf < EndBuf) {
364 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000365 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000366 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000367
368 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +0000369 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000370 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000371 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000372 break;
373
374 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000375 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000376 BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
377 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000378 break;
379 }
380
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000381 case BytecodeFormat::InstructionList: {
382 BCR_TRACE(2, "BLOCK BytecodeFormat::InstructionList: {\n");
383 if (BlockNum) throw std::string("Already parsed basic blocks!");
384 BlockNum = ParseInstructionList(F, Buf, Buf+Size);
385 break;
386 }
387
Chris Lattner29b789b2003-11-19 17:27:18 +0000388 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000389 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000390 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
Chris Lattner00950542001-06-06 20:29:01 +0000391 break;
392
393 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000394 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000395 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000396 if (OldBuf > Buf)
397 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000398 break;
399 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000400 BCR_TRACE(2, "} end block\n");
401
Misha Brukman12c29d12003-09-22 23:38:23 +0000402 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000403 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000404 }
405
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000406 // Make sure there were no references to non-existant basic blocks.
407 if (BlockNum != ParsedBasicBlocks.size())
408 throw std::string("Illegal basic block operand reference");
409 ParsedBasicBlocks.clear();
410
Chris Lattner29b789b2003-11-19 17:27:18 +0000411
Chris Lattner97330cf2003-10-09 23:10:14 +0000412 // Resolve forward references. Replace any uses of a forward reference value
413 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000414
Chris Lattner97330cf2003-10-09 23:10:14 +0000415 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
416 // number of operands. PHI nodes often have forward references, and can also
417 // often have a very large number of operands.
418 std::map<Value*, Value*> ForwardRefMapping;
419 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
420 I = ForwardReferences.begin(), E = ForwardReferences.end();
421 I != E; ++I)
422 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
423 false);
424
425 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
426 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
427 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
428 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
429 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
430 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
431 }
432
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000433 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +0000434 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
435 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000436 Value *PlaceHolder = I->second;
437 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +0000438
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000439 // Now that all the uses are gone, delete the placeholder...
440 // If we couldn't find a def (error case), then leak a little
441 // memory, because otherwise we can't remove all uses!
442 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +0000443 }
Chris Lattner00950542001-06-06 20:29:01 +0000444
Misha Brukman12c29d12003-09-22 23:38:23 +0000445 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000446 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000447
Chris Lattner52e20b02003-03-19 20:54:26 +0000448 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000449}
450
Misha Brukman12c29d12003-09-22 23:38:23 +0000451void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
452 const unsigned char *End) {
453 if (!FunctionSignatureList.empty())
454 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000455
Chris Lattner70cc3392001-09-10 07:58:01 +0000456 // Read global variables...
457 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000458 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000459 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000460 unsigned SlotNo;
461 GlobalValue::LinkageTypes Linkage;
462
463 if (!hasInternalMarkerOnly) {
Chris Lattner22482a12003-10-18 06:30:21 +0000464 unsigned LinkageID;
465 if (hasExtendedLinkageSpecs) {
466 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
467 // bit2,3,4 = Linkage, bit4+ = slot#
468 SlotNo = VarType >> 5;
469 LinkageID = (VarType >> 2) & 7;
470 } else {
471 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
472 // bit2,3 = Linkage, bit4+ = slot#
473 SlotNo = VarType >> 4;
474 LinkageID = (VarType >> 2) & 3;
475 }
476 switch (LinkageID) {
Chris Lattnerb91d9712003-10-18 19:48:10 +0000477 default: assert(0 && "Unknown linkage type!");
Chris Lattner6b252422003-10-16 18:28:50 +0000478 case 0: Linkage = GlobalValue::ExternalLinkage; break;
479 case 1: Linkage = GlobalValue::WeakLinkage; break;
480 case 2: Linkage = GlobalValue::AppendingLinkage; break;
481 case 3: Linkage = GlobalValue::InternalLinkage; break;
Chris Lattner22482a12003-10-18 06:30:21 +0000482 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Chris Lattner6b252422003-10-16 18:28:50 +0000483 }
Chris Lattnere3869c82003-04-16 21:16:05 +0000484 } else {
485 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
486 // bit2 = isInternal, bit3+ = slot#
487 SlotNo = VarType >> 3;
488 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
489 GlobalValue::ExternalLinkage;
490 }
491
492 const Type *Ty = getType(SlotNo);
Chris Lattner927b1852003-10-09 20:22:47 +0000493 if (!isa<PointerType>(Ty))
Misha Brukman12c29d12003-09-22 23:38:23 +0000494 throw std::string("Global not pointer type! Ty = " +
495 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000496
Chris Lattner52e20b02003-03-19 20:54:26 +0000497 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000498
Chris Lattner70cc3392001-09-10 07:58:01 +0000499 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000500 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000501 0, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000502 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner29b789b2003-11-19 17:27:18 +0000503 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +0000504
Misha Brukman37f92e22003-09-11 22:34:13 +0000505 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000506 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000507 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000508 GlobalInits.push_back(std::make_pair(GV, InitSlot));
509 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000510 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000511 }
512
Chris Lattner52e20b02003-03-19 20:54:26 +0000513 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000514 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000515 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000516 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
517 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +0000518 if (!isa<PointerType>(Ty) ||
519 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Misha Brukman12c29d12003-09-22 23:38:23 +0000520 throw std::string("Function not ptr to func type! Ty = " +
521 Ty->getDescription());
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000522
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000523 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000524 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000525
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000526 // When the ModuleGlobalInfo section is read, we load the type of each
527 // function and the 'ModuleValues' slot that it lands in. We then load a
528 // placeholder into its slot to reserve it. When the function is loaded,
529 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000530
531 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000532 Function *Func = new Function(cast<FunctionType>(Ty),
533 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +0000534 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000535
Chris Lattner52e20b02003-03-19 20:54:26 +0000536 // Keep track of this information in a list that is emptied as functions are
537 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000538 //
Chris Lattner29b789b2003-11-19 17:27:18 +0000539 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +0000540
Misha Brukman12c29d12003-09-22 23:38:23 +0000541 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000542 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000543 }
544
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000545 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000546
547 // Now that the function signature list is set up, reverse it so that we can
548 // remove elements efficiently from the back of the vector.
549 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000550
551 // This is for future proofing... in the future extra fields may be added that
552 // we don't understand, so we transparently ignore them.
553 //
554 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000555}
556
Misha Brukman12c29d12003-09-22 23:38:23 +0000557void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000558 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000559 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000560 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000561
562 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000563 Module::Endianness Endianness;
564 Module::PointerSize PointerSize;
565 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
566 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
567
568 bool hasNoEndianness = Version & 4;
569 bool hasNoPointerSize = Version & 8;
570
571 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000572
573 // Default values for the current bytecode version
Chris Lattnere3869c82003-04-16 21:16:05 +0000574 hasInternalMarkerOnly = false;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000575 hasExtendedLinkageSpecs = true;
576 hasOldStyleVarargs = false;
577 hasVarArgCallPadding = false;
Chris Lattnere3869c82003-04-16 21:16:05 +0000578 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000579
580 switch (RevisionNum) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000581 case 1: // LLVM pre-1.0 release: will be deleted on the next rev
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000582 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
583 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000584 hasInternalMarkerOnly = true;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000585 hasExtendedLinkageSpecs = false;
586 hasOldStyleVarargs = true;
587 hasVarArgCallPadding = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000588 break;
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000589 case 2: // LLVM pre-1.0 release:
Chris Lattnere3869c82003-04-16 21:16:05 +0000590 // Version #2 added information about all 4 linkage types instead of just
591 // having internal and external.
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000592 hasExtendedLinkageSpecs = false;
593 hasOldStyleVarargs = true;
594 hasVarArgCallPadding = true;
595 break;
596 case 0: // LLVM 1.0 release version
597 // Compared to rev #2, we added support for weak linkage, a more dense
598 // encoding, and better varargs support.
599
600 // FIXME: densify the encoding!
Chris Lattner036b8aa2003-03-06 17:55:45 +0000601 break;
602 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000603 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000604 }
605
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000606 if (hasNoEndianness) Endianness = Module::AnyEndianness;
607 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000608
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000609 TheModule->setEndianness(Endianness);
610 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000611 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000612 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
613 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000614}
615
Misha Brukman12c29d12003-09-22 23:38:23 +0000616void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000617 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000618 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000619 readBlock(Buf, EndBuf, Type, Size);
620 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
621 throw std::string("Expected Module packet! B: "+
622 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
623 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000624
Chris Lattner1d670cc2001-09-07 16:37:43 +0000625 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000626 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000627
628 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000629 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000630 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattner00950542001-06-06 20:29:01 +0000632 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000633 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000634 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000635 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000636 case BytecodeFormat::GlobalTypePlane:
637 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000638 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000639 break;
640
641 case BytecodeFormat::ModuleGlobalInfo:
642 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000643 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000644 break;
645
Chris Lattner1d670cc2001-09-07 16:37:43 +0000646 case BytecodeFormat::ConstantPool:
647 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000648 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000649 break;
650
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000651 case BytecodeFormat::Function: {
652 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000653 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000654 break;
655 }
656
657 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000658 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000659 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
Chris Lattner00950542001-06-06 20:29:01 +0000660 break;
Chris Lattner00950542001-06-06 20:29:01 +0000661 default:
Chris Lattner00950542001-06-06 20:29:01 +0000662 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000663 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000664 break;
665 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000666 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000667 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000668 }
669
Chris Lattner52e20b02003-03-19 20:54:26 +0000670 // After the module constant pool has been read, we can safely initialize
671 // global variables...
672 while (!GlobalInits.empty()) {
673 GlobalVariable *GV = GlobalInits.back().first;
674 unsigned Slot = GlobalInits.back().second;
675 GlobalInits.pop_back();
676
677 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +0000678 // FIXME: Preserve this type ID!
679 unsigned TypeSlot = getTypeSlot(GV->getType()->getElementType());
680 if (Value *V = getValue(TypeSlot, Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000681 if (GV->hasInitializer())
682 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000683 GV->setInitializer(cast<Constant>(V));
684 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000685 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000686 }
687
Misha Brukman12c29d12003-09-22 23:38:23 +0000688 if (!FunctionSignatureList.empty())
689 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000690
691 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000692}
693
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000694void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
695 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000696
Misha Brukman12c29d12003-09-22 23:38:23 +0000697 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000698
Chris Lattner00950542001-06-06 20:29:01 +0000699 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000700 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000701 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000702 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
703 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000704
Chris Lattner75f20532003-04-22 18:02:52 +0000705 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000706 try {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000707 usesOldStyleVarargs = false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000708 ParseModule(Buf, EndBuf);
709 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000710 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000711 delete TheModule;
712 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000713 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000714 }
Chris Lattner00950542001-06-06 20:29:01 +0000715}