blob: 071b1da40f534f72b485fb501b2ccc9db6f395e9 [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 Brukman12c29d12003-09-22 23:38:23 +000028#define CHECK_ALIGN32(begin,end) \
29 if (align32(begin,end)) \
30 throw std::string("Alignment error: Reader.cpp:" + \
31 utostr((unsigned)__LINE__));
32
33void
34BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
Chris Lattner00950542001-06-06 20:29:01 +000035 if (Ty->isPrimitiveType()) {
36 Slot = Ty->getPrimitiveID();
37 } else {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000038 // Check the function level types first...
Chris Lattner6e5a0e42003-03-06 17:18:14 +000039 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
Misha Brukman12c29d12003-09-22 23:38:23 +000040 FunctionTypeValues.end(), Ty);
Chris Lattner6e5a0e42003-03-06 17:18:14 +000041 if (I != FunctionTypeValues.end()) {
Misha Brukman12c29d12003-09-22 23:38:23 +000042 Slot = FirstDerivedTyID + ModuleTypeValues.size() +
43 (&*I - &FunctionTypeValues[0]);
Chris Lattner1d670cc2001-09-07 16:37:43 +000044 } else {
45 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +000046 if (I == ModuleTypeValues.end())
47 throw std::string("Didn't find type in ModuleTypeValues.");
Chris Lattner1d670cc2001-09-07 16:37:43 +000048 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
49 }
Chris Lattner00950542001-06-06 20:29:01 +000050 }
Chris Lattner697954c2002-01-20 22:54:45 +000051 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000052}
53
54const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000055 if (ID < Type::NumPrimitiveIDs) {
56 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
57 if (T) return T;
58 }
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattner697954c2002-01-20 22:54:45 +000060 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000061 const Value *V = getValue(Type::TypeTy, ID, false);
62 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000063}
64
Chris Lattner52e20b02003-03-19 20:54:26 +000065int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
66 assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
67 Val->getType()->isPrimitiveType() ||
68 !cast<Constant>(Val)->isNullValue()) &&
69 "Cannot read null values from bytecode!");
Chris Lattner00950542001-06-06 20:29:01 +000070 unsigned type;
Misha Brukman12c29d12003-09-22 23:38:23 +000071 getTypeSlot(Val->getType(), type);
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattner52e20b02003-03-19 20:54:26 +000074 if (ValueTab.size() <= type) {
75 unsigned OldSize = ValueTab.size();
76 ValueTab.resize(type+1);
77 while (OldSize != type+1)
78 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +000079 }
Chris Lattner00950542001-06-06 20:29:01 +000080
81 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000082 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000083 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattner52e20b02003-03-19 20:54:26 +000085 bool HasOffset = HasImplicitZeroInitializer &&
Misha Brukman12c29d12003-09-22 23:38:23 +000086 !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +000087
88 return ValueTab[type]->size()-1 + HasOffset;
89}
90
91
92void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
93 Value *Val) {
94 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
95 unsigned type;
Misha Brukman12c29d12003-09-22 23:38:23 +000096 getTypeSlot(Val->getType(), type);
97
Chris Lattner52e20b02003-03-19 20:54:26 +000098 assert((!HasImplicitZeroInitializer || Slot != 0) &&
99 "Cannot change zero init");
100 assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
101 ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000102}
103
104Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
105 unsigned Num = oNum;
106 unsigned type; // The type plane it lives in...
Misha Brukman12c29d12003-09-22 23:38:23 +0000107 getTypeSlot(Ty, type);
Chris Lattner00950542001-06-06 20:29:01 +0000108
Misha Brukman12c29d12003-09-22 23:38:23 +0000109 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +0000110 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000111 if (Num < Type::NumPrimitiveIDs) {
112 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
113 if (T) return (Value*)T; // Asked for a primitive type...
114 }
Chris Lattner00950542001-06-06 20:29:01 +0000115
116 // Otherwise, derived types need offset...
117 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000118
Misha Brukman12c29d12003-09-22 23:38:23 +0000119 // Is it a module-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000121 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
Misha Brukman12c29d12003-09-22 23:38:23 +0000123 // Nope, is it a function-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000124 Num -= ModuleTypeValues.size();
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000125 if (Num < FunctionTypeValues.size())
126 return (Value*)FunctionTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000127
128 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000129 }
130
Chris Lattner52e20b02003-03-19 20:54:26 +0000131 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
132 if (Num == 0)
133 return Constant::getNullValue(Ty);
134 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000135 }
136
Chris Lattner52e20b02003-03-19 20:54:26 +0000137 if (type < ModuleValues.size()) {
138 if (Num < ModuleValues[type]->size())
139 return ModuleValues[type]->getOperand(Num);
140 Num -= ModuleValues[type]->size();
141 }
142
143 if (Values.size() > type && Values[type]->size() > Num)
144 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000145
Chris Lattner74734132002-08-17 22:01:27 +0000146 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000147
148 Value *d = 0;
149 switch (Ty->getPrimitiveID()) {
Chris Lattner74734132002-08-17 22:01:27 +0000150 case Type::LabelTyID:
151 d = new BBPHolder(Ty, oNum);
152 break;
153 default:
154 d = new ValPHolder(Ty, oNum);
155 break;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
158 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000159 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000160 return d;
161}
162
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000163/// getConstantValue - Just like getValue, except that it returns a null pointer
164/// only on error. It always returns a constant (meaning that if the value is
165/// defined, but is not a constant, that is an error). If the specified
166/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
167/// after the real value is parsed, the placeholder is eliminated.
168///
169Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
170 if (Value *V = getValue(Ty, Slot, false))
171 return dyn_cast<Constant>(V); // If we already have the value parsed...
172
Chris Lattner52e20b02003-03-19 20:54:26 +0000173 std::pair<const Type*, unsigned> Key(Ty, Slot);
174 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
175
176 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000177 BCR_TRACE(5, "Previous forward ref found!\n");
178 return cast<Constant>(I->second);
179 } 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 Lattner52e20b02003-03-19 20:54:26 +0000186 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000187 return C;
188 }
189}
190
191
Misha Brukman12c29d12003-09-22 23:38:23 +0000192void BytecodeParser::postResolveValues(ValueTable &ValTab) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000193 while (!ValTab.empty()) {
194 ValueList &DL = *ValTab.back();
195 ValTab.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000196
Chris Lattner52e20b02003-03-19 20:54:26 +0000197 while (!DL.empty()) {
198 Value *D = DL.back();
199 unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
Chris Lattner00950542001-06-06 20:29:01 +0000200 DL.pop_back();
201
202 Value *NewDef = getValue(D->getType(), IDNumber, false);
203 if (NewDef == 0) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000204 throw std::string("Unresolvable reference found: <" +
205 D->getType()->getName() + ">:" +utostr(IDNumber)+".");
Chris Lattner00950542001-06-06 20:29:01 +0000206 } else {
Misha Brukman12c29d12003-09-22 23:38:23 +0000207 // Fixup all of the uses of this placeholder def...
Chris Lattner00950542001-06-06 20:29:01 +0000208 D->replaceAllUsesWith(NewDef);
209
210 // Now that all the uses are gone, delete the placeholder...
211 // If we couldn't find a def (error case), then leak a little
Misha Brukman12c29d12003-09-22 23:38:23 +0000212 delete D; // memory, 'cause otherwise we can't remove all uses!
Chris Lattner00950542001-06-06 20:29:01 +0000213 }
214 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000215 delete &DL;
Chris Lattner00950542001-06-06 20:29:01 +0000216 }
Chris Lattner00950542001-06-06 20:29:01 +0000217}
218
Misha Brukman12c29d12003-09-22 23:38:23 +0000219std::auto_ptr<BasicBlock>
220BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
221 const unsigned char *EndBuf) {
222 std::auto_ptr<BasicBlock> BB(new BasicBlock());
Chris Lattner00950542001-06-06 20:29:01 +0000223
224 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000225 Instruction *Inst;
Misha Brukman12c29d12003-09-22 23:38:23 +0000226 ParseInstruction(Buf, EndBuf, Inst);
227
228 if (Inst == 0) { throw std::string("Could not parse Instruction."); }
229 if (insertValue(Inst, Values) == -1) {
230 throw std::string("Could not insert value.");
Chris Lattner00950542001-06-06 20:29:01 +0000231 }
232
Chris Lattner1d670cc2001-09-07 16:37:43 +0000233 BB->getInstList().push_back(Inst);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000234 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000235 }
236
Misha Brukman12c29d12003-09-22 23:38:23 +0000237 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000238}
239
Misha Brukman12c29d12003-09-22 23:38:23 +0000240void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000241 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000242 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000243 while (Buf < EndBuf) {
244 // Symtab block header: [num entries][type id number]
245 unsigned NumEntries, Typ;
246 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000247 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000248 const Type *Ty = getType(Typ);
Misha Brukman12c29d12003-09-22 23:38:23 +0000249 if (Ty == 0) throw std::string("Invalid type read in symbol table.");
Chris Lattner00950542001-06-06 20:29:01 +0000250
Chris Lattner1d670cc2001-09-07 16:37:43 +0000251 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000252 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000253
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000254 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000255 // Symtab entry: [def slot #][name]
256 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000257 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000258 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000259 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Misha Brukman12c29d12003-09-22 23:38:23 +0000260 throw std::string("Buffer not aligned.");
Chris Lattner00950542001-06-06 20:29:01 +0000261
Chris Lattner52e20b02003-03-19 20:54:26 +0000262 Value *V = getValue(Ty, slot, false); // Find mapping...
263 if (V == 0) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000264 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
265 throw std::string("Failed value look-up.");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000266 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000267 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000268 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000269
Chris Lattner52e20b02003-03-19 20:54:26 +0000270 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000271 }
272 }
273
Misha Brukman12c29d12003-09-22 23:38:23 +0000274 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000275}
276
Chris Lattner74734132002-08-17 22:01:27 +0000277void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000278 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
279 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000280 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000281
Chris Lattner74734132002-08-17 22:01:27 +0000282 BCR_TRACE(3, "Mutating forward refs!\n");
283 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000284
Chris Lattner52e20b02003-03-19 20:54:26 +0000285 VPH->replaceAllUsesWith(NewV);
286
287 // If this is a global variable being resolved, remove the placeholder from
288 // the module...
289 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
290 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000291
Chris Lattner74734132002-08-17 22:01:27 +0000292 delete VPH; // Delete the old placeholder
293 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000294}
295
Misha Brukman12c29d12003-09-22 23:38:23 +0000296void
297BytecodeParser::ParseFunction(const unsigned char *&Buf,
298 const unsigned char *EndBuf) {
299 if (FunctionSignatureList.empty())
300 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000301
Misha Brukman12c29d12003-09-22 23:38:23 +0000302 Function *F = FunctionSignatureList.back().first;
303 unsigned FunctionSlot = FunctionSignatureList.back().second;
304 FunctionSignatureList.pop_back();
305
306 // Save the information for future reading of the function
307 LazyFunctionInfo *LFI = new LazyFunctionInfo();
308 LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
309 LazyFunctionLoadMap[F] = LFI;
310 // Pretend we've `parsed' this function
311 Buf = EndBuf;
312}
313
314void BytecodeParser::materializeFunction(Function* F) {
315 // Find {start, end} pointers and slot in the map. If not there, we're done.
316 std::map<Function*, LazyFunctionInfo*>::iterator Fi =
317 LazyFunctionLoadMap.find(F);
318 if (Fi == LazyFunctionLoadMap.end()) return;
319
320 LazyFunctionInfo *LFI = Fi->second;
321 const unsigned char *Buf = LFI->Buf;
322 const unsigned char *EndBuf = LFI->EndBuf;
323 unsigned FunctionSlot = LFI->FunctionSlot;
324 LazyFunctionLoadMap.erase(Fi);
325 delete LFI;
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) {
330 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000331 if (read_vbr(Buf, EndBuf, LinkageType))
332 throw std::string("ParseFunction: Error reading from buffer.");
333 if (LinkageType & ~0x3)
334 throw std::string("Invalid linkage type for Function.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000335 Linkage = (GlobalValue::LinkageTypes)LinkageType;
336 } else {
337 // We used to only support two linkage models: internal and external
338 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000339 if (read_vbr(Buf, EndBuf, isInternal))
340 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000341 if (isInternal) Linkage = GlobalValue::InternalLinkage;
342 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000343
Chris Lattnere3869c82003-04-16 21:16:05 +0000344 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000345
Chris Lattner52e20b02003-03-19 20:54:26 +0000346 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
347 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000348 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000349 It != Params.end(); ++It, ++AI) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000350 if (insertValue(AI, Values) == -1)
351 throw std::string("Error reading function arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000352 }
353
354 while (Buf < EndBuf) {
355 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000356 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000357 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000358
359 switch (Type) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000360 case BytecodeFormat::ConstantPool: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000361 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000362 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000363 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000364 }
Chris Lattner00950542001-06-06 20:29:01 +0000365
366 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000367 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000368 std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
369 if (!BB.get() || insertValue(BB.get(), Values) == -1)
370 throw std::string("Parse error: BasicBlock");
Chris Lattner00950542001-06-06 20:29:01 +0000371
Misha Brukman12c29d12003-09-22 23:38:23 +0000372 F->getBasicBlockList().push_back(BB.release());
Chris Lattner00950542001-06-06 20:29:01 +0000373 break;
374 }
375
Misha Brukman12c29d12003-09-22 23:38:23 +0000376 case BytecodeFormat::SymbolTable: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000377 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000378 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000379 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000380 }
Chris Lattner00950542001-06-06 20:29:01 +0000381
382 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000383 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000384 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000385 if (OldBuf > Buf)
386 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000387 break;
388 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000389 BCR_TRACE(2, "} end block\n");
390
Misha Brukman12c29d12003-09-22 23:38:23 +0000391 // Malformed bc file if read past end of block.
392 CHECK_ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000393 }
394
Misha Brukman12c29d12003-09-22 23:38:23 +0000395 // Check for unresolvable references
396 postResolveValues(LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000397
Misha Brukman12c29d12003-09-22 23:38:23 +0000398 //ResolveReferencesToValue(F, FunctionSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000399
Misha Brukman12c29d12003-09-22 23:38:23 +0000400 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000401 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000402
Chris Lattner52e20b02003-03-19 20:54:26 +0000403 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000404}
405
Misha Brukman12c29d12003-09-22 23:38:23 +0000406void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
407 const unsigned char *End) {
408 if (!FunctionSignatureList.empty())
409 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000410
Chris Lattner70cc3392001-09-10 07:58:01 +0000411 // Read global variables...
412 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000413 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000414 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000415 unsigned SlotNo;
416 GlobalValue::LinkageTypes Linkage;
417
418 if (!hasInternalMarkerOnly) {
419 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
420 // bit2,3 = Linkage, bit4+ = slot#
421 SlotNo = VarType >> 4;
422 Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
423 } else {
424 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
425 // bit2 = isInternal, bit3+ = slot#
426 SlotNo = VarType >> 3;
427 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
428 GlobalValue::ExternalLinkage;
429 }
430
431 const Type *Ty = getType(SlotNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000432 if (!Ty || !isa<PointerType>(Ty))
433 throw std::string("Global not pointer type! Ty = " +
434 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000435
Chris Lattner52e20b02003-03-19 20:54:26 +0000436 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000437
Chris Lattner70cc3392001-09-10 07:58:01 +0000438 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000439 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000440 0, "", TheModule);
Chris Lattner05950c32001-10-13 06:47:01 +0000441 int DestSlot = insertValue(GV, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000442 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000443 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000444 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000445
Misha Brukman37f92e22003-09-11 22:34:13 +0000446 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000447 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000448 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000449 GlobalInits.push_back(std::make_pair(GV, InitSlot));
450 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000451 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000452 }
453
Chris Lattner52e20b02003-03-19 20:54:26 +0000454 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000455 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000456 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000457 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
458 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000459 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000460 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000461 throw std::string("Function not ptr to func type! Ty = " +
462 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000463 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000464
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000465 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000466 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000467
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000468 // When the ModuleGlobalInfo section is read, we load the type of each
469 // function and the 'ModuleValues' slot that it lands in. We then load a
470 // placeholder into its slot to reserve it. When the function is loaded,
471 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000472
473 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000474 Function *Func = new Function(cast<FunctionType>(Ty),
475 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000476 int DestSlot = insertValue(Func, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000477 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000478 ResolveReferencesToValue(Func, (unsigned)DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000479
Chris Lattner52e20b02003-03-19 20:54:26 +0000480 // Keep track of this information in a list that is emptied as functions are
481 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000482 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000483 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
484
Misha Brukman12c29d12003-09-22 23:38:23 +0000485 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000486 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000487 }
488
Misha Brukman12c29d12003-09-22 23:38:23 +0000489 CHECK_ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000490
491 // Now that the function signature list is set up, reverse it so that we can
492 // remove elements efficiently from the back of the vector.
493 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000494
495 // This is for future proofing... in the future extra fields may be added that
496 // we don't understand, so we transparently ignore them.
497 //
498 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000499}
500
Misha Brukman12c29d12003-09-22 23:38:23 +0000501void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000502 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000503 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000504 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000505
506 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000507 Module::Endianness Endianness;
508 Module::PointerSize PointerSize;
509 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
510 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
511
512 bool hasNoEndianness = Version & 4;
513 bool hasNoPointerSize = Version & 8;
514
515 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000516
517 // Default values for the current bytecode version
Chris Lattner036b8aa2003-03-06 17:55:45 +0000518 HasImplicitZeroInitializer = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000519 hasInternalMarkerOnly = false;
520 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000521
522 switch (RevisionNum) {
523 case 0: // Initial revision
Chris Lattner52e20b02003-03-19 20:54:26 +0000524 // Version #0 didn't have any of the flags stored correctly, and in fact as
525 // only valid with a 14 in the flags values. Also, it does not support
526 // encoding zero initializers for arrays compactly.
527 //
Misha Brukman12c29d12003-09-22 23:38:23 +0000528 if (Version != 14) throw std::string("Unknown revision 0 flags?");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000529 HasImplicitZeroInitializer = false;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000530 Endianness = Module::BigEndian;
531 PointerSize = Module::Pointer64;
Chris Lattnere3869c82003-04-16 21:16:05 +0000532 hasInternalMarkerOnly = true;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000533 hasNoEndianness = hasNoPointerSize = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000534 break;
535 case 1:
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000536 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
537 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000538 hasInternalMarkerOnly = true;
539 break;
540 case 2:
541 // Version #2 added information about all 4 linkage types instead of just
542 // having internal and external.
Chris Lattner036b8aa2003-03-06 17:55:45 +0000543 break;
544 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000545 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000546 }
547
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000548 if (hasNoEndianness) Endianness = Module::AnyEndianness;
549 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000550
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000551 TheModule->setEndianness(Endianness);
552 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000553 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000554 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
555 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000556 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000557}
558
Misha Brukman12c29d12003-09-22 23:38:23 +0000559void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000560 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000561 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000562 readBlock(Buf, EndBuf, Type, Size);
563 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
564 throw std::string("Expected Module packet! B: "+
565 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
566 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000567
Chris Lattner1d670cc2001-09-07 16:37:43 +0000568 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000569 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000570
571 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000572 ParseVersionInfo(Buf, EndBuf);
573 CHECK_ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000574
Chris Lattner00950542001-06-06 20:29:01 +0000575 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000576 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000577 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000578 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000579 case BytecodeFormat::GlobalTypePlane:
580 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000581 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000582 break;
583
584 case BytecodeFormat::ModuleGlobalInfo:
585 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000586 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000587 break;
588
Chris Lattner1d670cc2001-09-07 16:37:43 +0000589 case BytecodeFormat::ConstantPool:
590 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000591 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000592 break;
593
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000594 case BytecodeFormat::Function: {
595 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000596 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000597 break;
598 }
599
600 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000601 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000602 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000603 break;
604
605 default:
Chris Lattner00950542001-06-06 20:29:01 +0000606 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000607 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000608 break;
609 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000610 BCR_TRACE(1, "} end block\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000611 CHECK_ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000612 }
613
Chris Lattner52e20b02003-03-19 20:54:26 +0000614 // After the module constant pool has been read, we can safely initialize
615 // global variables...
616 while (!GlobalInits.empty()) {
617 GlobalVariable *GV = GlobalInits.back().first;
618 unsigned Slot = GlobalInits.back().second;
619 GlobalInits.pop_back();
620
621 // Look up the initializer value...
622 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000623 if (GV->hasInitializer())
624 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000625 GV->setInitializer(cast<Constant>(V));
626 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000627 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000628 }
629
Misha Brukman12c29d12003-09-22 23:38:23 +0000630 if (!FunctionSignatureList.empty())
631 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000632
633 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000634}
635
Misha Brukman12c29d12003-09-22 23:38:23 +0000636void
637BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
638 const std::string &ModuleID) {
Chris Lattner00950542001-06-06 20:29:01 +0000639 unsigned Sig;
Misha Brukman12c29d12003-09-22 23:38:23 +0000640 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Chris Lattner00950542001-06-06 20:29:01 +0000641 // Read and check signature...
642 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000643 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
644 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000645
Chris Lattner75f20532003-04-22 18:02:52 +0000646 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000647 try {
648 ParseModule(Buf, EndBuf);
649 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000650 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000651 delete TheModule;
652 TheModule = 0;
Misha Brukman12c29d12003-09-22 23:38:23 +0000653 throw Error;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000654 }
Chris Lattner00950542001-06-06 20:29:01 +0000655}