blob: 29812da561ee91cfa4f6790790f6239464bf6a6b [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Bytecode/Reader.h
4//
5// Note that this library should be as fast as possible, reentrant, and
6// threadsafe!!
7//
Chris Lattner74734132002-08-17 22:01:27 +00008// TODO: Return error messages to caller instead of printing them out directly.
Chris Lattner00950542001-06-06 20:29:01 +00009// TODO: Allow passing in an option to ignore the symbol table
10//
Chris Lattnerd6b65252001-10-24 01:15:12 +000011//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000012
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Bytecode/Reader.h"
15#include "llvm/Bytecode/Format.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000017#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iOther.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000019#include "llvm/Module.h"
20#include "Support/StringExtras.h"
John Criswell7a73b802003-06-30 21:59:07 +000021#include "Config/unistd.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000022#include "Config/sys/mman.h"
23#include "Config/sys/stat.h"
24#include "Config/sys/types.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include <algorithm>
Misha Brukman12c29d12003-09-22 23:38:23 +000026#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000027
Misha Brukmane0dd0d42003-09-23 16:15:29 +000028static inline void ALIGN32(const unsigned char *&begin,
29 const unsigned char *end) {
30 if (align32(begin, end))
31 throw std::string("Alignment error in buffer: read past end of block.");
32}
Misha Brukman12c29d12003-09-22 23:38:23 +000033
Chris Lattner9e460f22003-10-04 20:00:03 +000034unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
35 if (Ty->isPrimitiveType())
36 return Ty->getPrimitiveID();
37
38 // Check the function level types first...
39 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
40 FunctionTypeValues.end(), Ty);
41 if (I != FunctionTypeValues.end())
42 return FirstDerivedTyID + ModuleTypeValues.size() +
43 (&*I - &FunctionTypeValues[0]);
44
45 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
46 if (I == ModuleTypeValues.end())
47 throw std::string("Didn't find type in ModuleTypeValues.");
48 return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000049}
50
51const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000052 if (ID < Type::NumPrimitiveIDs) {
53 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
54 if (T) return T;
55 }
Chris Lattner00950542001-06-06 20:29:01 +000056
Chris Lattner697954c2002-01-20 22:54:45 +000057 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner36392bc2003-10-08 21:18:57 +000058
59 if (ID < Type::NumPrimitiveIDs) {
60 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
61 if (T) return T; // Asked for a primitive type...
62 }
63
64 // Otherwise, derived types need offset...
65 ID -= FirstDerivedTyID;
66
67 // Is it a module-level type?
68 if (ID < ModuleTypeValues.size())
69 return ModuleTypeValues[ID].get();
70
71 // Nope, is it a function-level type?
72 ID -= ModuleTypeValues.size();
73 if (ID < FunctionTypeValues.size())
74 return FunctionTypeValues[ID].get();
75
76 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000077}
78
Chris Lattner52e20b02003-03-19 20:54:26 +000079int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
80 assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
81 Val->getType()->isPrimitiveType() ||
82 !cast<Constant>(Val)->isNullValue()) &&
83 "Cannot read null values from bytecode!");
Chris Lattner9e460f22003-10-04 20:00:03 +000084 unsigned type = getTypeSlot(Val->getType());
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000086
Chris Lattner52e20b02003-03-19 20:54:26 +000087 if (ValueTab.size() <= type) {
88 unsigned OldSize = ValueTab.size();
89 ValueTab.resize(type+1);
90 while (OldSize != type+1)
91 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +000092 }
Chris Lattner00950542001-06-06 20:29:01 +000093
94 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000095 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000096 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000097
Chris Lattner52e20b02003-03-19 20:54:26 +000098 bool HasOffset = HasImplicitZeroInitializer &&
Misha Brukman12c29d12003-09-22 23:38:23 +000099 !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +0000100
101 return ValueTab[type]->size()-1 + HasOffset;
102}
103
104
105void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
106 Value *Val) {
107 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000108 assert((!HasImplicitZeroInitializer || Slot != 0) &&
109 "Cannot change zero init");
Chris Lattner9e460f22003-10-04 20:00:03 +0000110 unsigned type = getTypeSlot(Val->getType());
Chris Lattner52e20b02003-03-19 20:54:26 +0000111 assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
112 ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000113}
114
Chris Lattner36392bc2003-10-08 21:18:57 +0000115
Chris Lattner00950542001-06-06 20:29:01 +0000116Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
Chris Lattner36392bc2003-10-08 21:18:57 +0000117 return getValue(getTypeSlot(Ty), oNum, Create);
118}
119
120Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
121 assert(type != Type::TypeTyID && "getValue() cannot get types!");
Chris Lattner00950542001-06-06 20:29:01 +0000122 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000123
Chris Lattner52e20b02003-03-19 20:54:26 +0000124 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
125 if (Num == 0)
Chris Lattner36392bc2003-10-08 21:18:57 +0000126 return Constant::getNullValue(getType(type));
Chris Lattner52e20b02003-03-19 20:54:26 +0000127 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000128 }
129
Chris Lattner52e20b02003-03-19 20:54:26 +0000130 if (type < ModuleValues.size()) {
131 if (Num < ModuleValues[type]->size())
132 return ModuleValues[type]->getOperand(Num);
133 Num -= ModuleValues[type]->size();
134 }
135
136 if (Values.size() > type && Values[type]->size() > Num)
137 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattner74734132002-08-17 22:01:27 +0000139 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner36392bc2003-10-08 21:18:57 +0000141 const Type *Ty = getType(type);
142 Value *Val = type == Type::LabelTyID ? (Value*)new BBPHolder(Ty, oNum) :
143 (Value*)new ValPHolder(Ty, oNum);
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattner36392bc2003-10-08 21:18:57 +0000145 assert(Val != 0 && "How did we not make something?");
146 if (insertValue(Val, LateResolveValues) == -1) return 0;
147 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000148}
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///
156Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
157 if (Value *V = getValue(Ty, Slot, false))
158 return dyn_cast<Constant>(V); // If we already have the value parsed...
159
Chris Lattner52e20b02003-03-19 20:54:26 +0000160 std::pair<const Type*, unsigned> Key(Ty, Slot);
161 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
162
163 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000164 BCR_TRACE(5, "Previous forward ref found!\n");
165 return cast<Constant>(I->second);
166 } else {
167 // Create a placeholder for the constant reference and
168 // keep track of the fact that we have a forward ref to recycle it
169 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
170 Constant *C = new ConstPHolder(Ty, Slot);
171
172 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner52e20b02003-03-19 20:54:26 +0000173 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000174 return C;
175 }
176}
177
178
Misha Brukman12c29d12003-09-22 23:38:23 +0000179std::auto_ptr<BasicBlock>
180BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
181 const unsigned char *EndBuf) {
182 std::auto_ptr<BasicBlock> BB(new BasicBlock());
Chris Lattner00950542001-06-06 20:29:01 +0000183
184 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000185 Instruction *Inst;
Misha Brukman12c29d12003-09-22 23:38:23 +0000186 ParseInstruction(Buf, EndBuf, Inst);
187
188 if (Inst == 0) { throw std::string("Could not parse Instruction."); }
189 if (insertValue(Inst, Values) == -1) {
190 throw std::string("Could not insert value.");
Chris Lattner00950542001-06-06 20:29:01 +0000191 }
192
Chris Lattner1d670cc2001-09-07 16:37:43 +0000193 BB->getInstList().push_back(Inst);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000194 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000195 }
196
Misha Brukman12c29d12003-09-22 23:38:23 +0000197 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000198}
199
Misha Brukman12c29d12003-09-22 23:38:23 +0000200void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000201 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000202 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000203 while (Buf < EndBuf) {
204 // Symtab block header: [num entries][type id number]
205 unsigned NumEntries, Typ;
206 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000207 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000208 const Type *Ty = getType(Typ);
Misha Brukman12c29d12003-09-22 23:38:23 +0000209 if (Ty == 0) throw std::string("Invalid type read in symbol table.");
Chris Lattner00950542001-06-06 20:29:01 +0000210
Chris Lattner1d670cc2001-09-07 16:37:43 +0000211 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000212 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000213
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000214 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000215 // Symtab entry: [def slot #][name]
216 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000217 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000218 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000219 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Misha Brukman12c29d12003-09-22 23:38:23 +0000220 throw std::string("Buffer not aligned.");
Chris Lattner00950542001-06-06 20:29:01 +0000221
Chris Lattner36392bc2003-10-08 21:18:57 +0000222 Value *V;
223 if (Typ == Type::TypeTyID)
224 V = (Value*)getType(slot);
225 else
226 V = getValue(Typ, slot, false); // Find mapping...
227 if (V == 0) throw std::string("Failed value look-up.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000228 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000229 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000230
Chris Lattner52e20b02003-03-19 20:54:26 +0000231 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000232 }
233 }
234
Misha Brukman12c29d12003-09-22 23:38:23 +0000235 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000236}
237
Chris Lattner74734132002-08-17 22:01:27 +0000238void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000239 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
240 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000241 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000242
Chris Lattner74734132002-08-17 22:01:27 +0000243 BCR_TRACE(3, "Mutating forward refs!\n");
244 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000245
Chris Lattner52e20b02003-03-19 20:54:26 +0000246 VPH->replaceAllUsesWith(NewV);
247
248 // If this is a global variable being resolved, remove the placeholder from
249 // the module...
250 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
251 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000252
Chris Lattner74734132002-08-17 22:01:27 +0000253 delete VPH; // Delete the old placeholder
254 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000255}
256
Misha Brukman12c29d12003-09-22 23:38:23 +0000257void
258BytecodeParser::ParseFunction(const unsigned char *&Buf,
259 const unsigned char *EndBuf) {
260 if (FunctionSignatureList.empty())
261 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000262
Misha Brukman12c29d12003-09-22 23:38:23 +0000263 Function *F = FunctionSignatureList.back().first;
264 unsigned FunctionSlot = FunctionSignatureList.back().second;
265 FunctionSignatureList.pop_back();
266
267 // Save the information for future reading of the function
268 LazyFunctionInfo *LFI = new LazyFunctionInfo();
269 LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
270 LazyFunctionLoadMap[F] = LFI;
271 // Pretend we've `parsed' this function
272 Buf = EndBuf;
273}
274
275void BytecodeParser::materializeFunction(Function* F) {
276 // Find {start, end} pointers and slot in the map. If not there, we're done.
277 std::map<Function*, LazyFunctionInfo*>::iterator Fi =
278 LazyFunctionLoadMap.find(F);
279 if (Fi == LazyFunctionLoadMap.end()) return;
280
281 LazyFunctionInfo *LFI = Fi->second;
282 const unsigned char *Buf = LFI->Buf;
283 const unsigned char *EndBuf = LFI->EndBuf;
284 unsigned FunctionSlot = LFI->FunctionSlot;
285 LazyFunctionLoadMap.erase(Fi);
286 delete LFI;
Chris Lattner00950542001-06-06 20:29:01 +0000287
Chris Lattnere3869c82003-04-16 21:16:05 +0000288 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
289
290 if (!hasInternalMarkerOnly) {
291 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000292 if (read_vbr(Buf, EndBuf, LinkageType))
293 throw std::string("ParseFunction: Error reading from buffer.");
294 if (LinkageType & ~0x3)
295 throw std::string("Invalid linkage type for Function.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000296 Linkage = (GlobalValue::LinkageTypes)LinkageType;
297 } else {
298 // We used to only support two linkage models: internal and external
299 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000300 if (read_vbr(Buf, EndBuf, isInternal))
301 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000302 if (isInternal) Linkage = GlobalValue::InternalLinkage;
303 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000304
Chris Lattnere3869c82003-04-16 21:16:05 +0000305 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000306
Chris Lattner52e20b02003-03-19 20:54:26 +0000307 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
308 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000309 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000310 It != Params.end(); ++It, ++AI) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000311 if (insertValue(AI, Values) == -1)
312 throw std::string("Error reading function arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000313 }
314
315 while (Buf < EndBuf) {
316 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000317 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000318 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000319
320 switch (Type) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000321 case BytecodeFormat::ConstantPool: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000322 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000323 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000324 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000325 }
Chris Lattner00950542001-06-06 20:29:01 +0000326
327 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000328 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000329 std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
330 if (!BB.get() || insertValue(BB.get(), Values) == -1)
331 throw std::string("Parse error: BasicBlock");
Chris Lattner00950542001-06-06 20:29:01 +0000332
Misha Brukman12c29d12003-09-22 23:38:23 +0000333 F->getBasicBlockList().push_back(BB.release());
Chris Lattner00950542001-06-06 20:29:01 +0000334 break;
335 }
336
Misha Brukman12c29d12003-09-22 23:38:23 +0000337 case BytecodeFormat::SymbolTable: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000338 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000339 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000340 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000341 }
Chris Lattner00950542001-06-06 20:29:01 +0000342
343 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000344 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000345 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000346 if (OldBuf > Buf)
347 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000348 break;
349 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000350 BCR_TRACE(2, "} end block\n");
351
Misha Brukman12c29d12003-09-22 23:38:23 +0000352 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000353 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000354 }
355
Misha Brukman12c29d12003-09-22 23:38:23 +0000356 // Check for unresolvable references
Chris Lattner6e448022003-10-08 21:51:46 +0000357 while (!LateResolveValues.empty()) {
358 ValueList &VL = *LateResolveValues.back();
359 LateResolveValues.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000360
Chris Lattner6e448022003-10-08 21:51:46 +0000361 while (!VL.empty()) {
362 Value *V = VL.back();
363 unsigned IDNumber = getValueIDNumberFromPlaceHolder(V);
364 VL.pop_back();
365
366 Value *NewVal = getValue(V->getType(), IDNumber, false);
367 if (NewVal == 0)
368 throw std::string("Unresolvable reference found: <" +
369 V->getType()->getDescription() + ">:" +
370 utostr(IDNumber) + ".");
371
372 // Fixup all of the uses of this placeholder def...
373 V->replaceAllUsesWith(NewVal);
374
375 // Now that all the uses are gone, delete the placeholder...
376 // If we couldn't find a def (error case), then leak a little
377 // memory, because otherwise we can't remove all uses!
378 delete V;
379 }
380 delete &VL;
381 }
Chris Lattner00950542001-06-06 20:29:01 +0000382
Misha Brukman12c29d12003-09-22 23:38:23 +0000383 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000384 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000385
Chris Lattner52e20b02003-03-19 20:54:26 +0000386 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000387}
388
Misha Brukman12c29d12003-09-22 23:38:23 +0000389void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
390 const unsigned char *End) {
391 if (!FunctionSignatureList.empty())
392 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000393
Chris Lattner70cc3392001-09-10 07:58:01 +0000394 // Read global variables...
395 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000396 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000397 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000398 unsigned SlotNo;
399 GlobalValue::LinkageTypes Linkage;
400
401 if (!hasInternalMarkerOnly) {
402 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
403 // bit2,3 = Linkage, bit4+ = slot#
404 SlotNo = VarType >> 4;
405 Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
406 } else {
407 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
408 // bit2 = isInternal, bit3+ = slot#
409 SlotNo = VarType >> 3;
410 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
411 GlobalValue::ExternalLinkage;
412 }
413
414 const Type *Ty = getType(SlotNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000415 if (!Ty || !isa<PointerType>(Ty))
416 throw std::string("Global not pointer type! Ty = " +
417 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000418
Chris Lattner52e20b02003-03-19 20:54:26 +0000419 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000420
Chris Lattner70cc3392001-09-10 07:58:01 +0000421 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000422 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000423 0, "", TheModule);
Chris Lattner05950c32001-10-13 06:47:01 +0000424 int DestSlot = insertValue(GV, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000425 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000426 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000427 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000428
Misha Brukman37f92e22003-09-11 22:34:13 +0000429 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000430 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000431 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000432 GlobalInits.push_back(std::make_pair(GV, InitSlot));
433 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000434 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000435 }
436
Chris Lattner52e20b02003-03-19 20:54:26 +0000437 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000438 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000439 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000440 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
441 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000442 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000443 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000444 throw std::string("Function not ptr to func type! Ty = " +
445 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000446 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000447
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000448 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000449 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000450
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000451 // When the ModuleGlobalInfo section is read, we load the type of each
452 // function and the 'ModuleValues' slot that it lands in. We then load a
453 // placeholder into its slot to reserve it. When the function is loaded,
454 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000455
456 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000457 Function *Func = new Function(cast<FunctionType>(Ty),
458 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000459 int DestSlot = insertValue(Func, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000460 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000461 ResolveReferencesToValue(Func, (unsigned)DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000462
Chris Lattner52e20b02003-03-19 20:54:26 +0000463 // Keep track of this information in a list that is emptied as functions are
464 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000465 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000466 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
467
Misha Brukman12c29d12003-09-22 23:38:23 +0000468 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000469 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000470 }
471
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000472 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000473
474 // Now that the function signature list is set up, reverse it so that we can
475 // remove elements efficiently from the back of the vector.
476 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000477
478 // This is for future proofing... in the future extra fields may be added that
479 // we don't understand, so we transparently ignore them.
480 //
481 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000482}
483
Misha Brukman12c29d12003-09-22 23:38:23 +0000484void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000485 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000486 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000487 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000488
489 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000490 Module::Endianness Endianness;
491 Module::PointerSize PointerSize;
492 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
493 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
494
495 bool hasNoEndianness = Version & 4;
496 bool hasNoPointerSize = Version & 8;
497
498 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000499
500 // Default values for the current bytecode version
Chris Lattner036b8aa2003-03-06 17:55:45 +0000501 HasImplicitZeroInitializer = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000502 hasInternalMarkerOnly = false;
503 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000504
505 switch (RevisionNum) {
506 case 0: // Initial revision
Chris Lattner52e20b02003-03-19 20:54:26 +0000507 // Version #0 didn't have any of the flags stored correctly, and in fact as
508 // only valid with a 14 in the flags values. Also, it does not support
509 // encoding zero initializers for arrays compactly.
510 //
Misha Brukman12c29d12003-09-22 23:38:23 +0000511 if (Version != 14) throw std::string("Unknown revision 0 flags?");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000512 HasImplicitZeroInitializer = false;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000513 Endianness = Module::BigEndian;
514 PointerSize = Module::Pointer64;
Chris Lattnere3869c82003-04-16 21:16:05 +0000515 hasInternalMarkerOnly = true;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000516 hasNoEndianness = hasNoPointerSize = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000517 break;
518 case 1:
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000519 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
520 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000521 hasInternalMarkerOnly = true;
522 break;
523 case 2:
524 // Version #2 added information about all 4 linkage types instead of just
525 // having internal and external.
Chris Lattner036b8aa2003-03-06 17:55:45 +0000526 break;
527 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000528 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000529 }
530
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000531 if (hasNoEndianness) Endianness = Module::AnyEndianness;
532 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000533
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000534 TheModule->setEndianness(Endianness);
535 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000536 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000537 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
538 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000539 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000540}
541
Misha Brukman12c29d12003-09-22 23:38:23 +0000542void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000543 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000544 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000545 readBlock(Buf, EndBuf, Type, Size);
546 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
547 throw std::string("Expected Module packet! B: "+
548 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
549 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000550
Chris Lattner1d670cc2001-09-07 16:37:43 +0000551 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000552 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000553
554 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000555 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000556 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000557
Chris Lattner00950542001-06-06 20:29:01 +0000558 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000559 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000560 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000561 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000562 case BytecodeFormat::GlobalTypePlane:
563 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000564 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000565 break;
566
567 case BytecodeFormat::ModuleGlobalInfo:
568 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000569 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000570 break;
571
Chris Lattner1d670cc2001-09-07 16:37:43 +0000572 case BytecodeFormat::ConstantPool:
573 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000574 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000575 break;
576
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000577 case BytecodeFormat::Function: {
578 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000579 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000580 break;
581 }
582
583 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000584 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000585 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000586 break;
587
588 default:
Chris Lattner00950542001-06-06 20:29:01 +0000589 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000590 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000591 break;
592 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000593 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000594 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000595 }
596
Chris Lattner52e20b02003-03-19 20:54:26 +0000597 // After the module constant pool has been read, we can safely initialize
598 // global variables...
599 while (!GlobalInits.empty()) {
600 GlobalVariable *GV = GlobalInits.back().first;
601 unsigned Slot = GlobalInits.back().second;
602 GlobalInits.pop_back();
603
604 // Look up the initializer value...
605 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000606 if (GV->hasInitializer())
607 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000608 GV->setInitializer(cast<Constant>(V));
609 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000610 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000611 }
612
Misha Brukman12c29d12003-09-22 23:38:23 +0000613 if (!FunctionSignatureList.empty())
614 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000615
616 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000617}
618
Misha Brukman12c29d12003-09-22 23:38:23 +0000619void
620BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
621 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000622
Misha Brukman12c29d12003-09-22 23:38:23 +0000623 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000624
Chris Lattner00950542001-06-06 20:29:01 +0000625 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000626 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000627 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000628 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
629 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000630
Chris Lattner75f20532003-04-22 18:02:52 +0000631 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000632 try {
633 ParseModule(Buf, EndBuf);
634 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000635 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000636 delete TheModule;
637 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000638 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000639 }
Chris Lattner00950542001-06-06 20:29:01 +0000640}