blob: 321a26f4c4e71c973cc6978a7697662e8e225780 [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
34void
35BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
Chris Lattner00950542001-06-06 20:29:01 +000036 if (Ty->isPrimitiveType()) {
37 Slot = Ty->getPrimitiveID();
38 } else {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000039 // Check the function level types first...
Chris Lattner6e5a0e42003-03-06 17:18:14 +000040 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
Misha Brukman12c29d12003-09-22 23:38:23 +000041 FunctionTypeValues.end(), Ty);
Chris Lattner6e5a0e42003-03-06 17:18:14 +000042 if (I != FunctionTypeValues.end()) {
Misha Brukman12c29d12003-09-22 23:38:23 +000043 Slot = FirstDerivedTyID + ModuleTypeValues.size() +
44 (&*I - &FunctionTypeValues[0]);
Chris Lattner1d670cc2001-09-07 16:37:43 +000045 } else {
46 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
Misha Brukman12c29d12003-09-22 23:38:23 +000047 if (I == ModuleTypeValues.end())
48 throw std::string("Didn't find type in ModuleTypeValues.");
Chris Lattner1d670cc2001-09-07 16:37:43 +000049 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
50 }
Chris Lattner00950542001-06-06 20:29:01 +000051 }
Chris Lattner697954c2002-01-20 22:54:45 +000052 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000053}
54
55const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000056 if (ID < Type::NumPrimitiveIDs) {
57 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
58 if (T) return T;
59 }
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner697954c2002-01-20 22:54:45 +000061 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000062 const Value *V = getValue(Type::TypeTy, ID, false);
63 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000064}
65
Chris Lattner52e20b02003-03-19 20:54:26 +000066int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
67 assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
68 Val->getType()->isPrimitiveType() ||
69 !cast<Constant>(Val)->isNullValue()) &&
70 "Cannot read null values from bytecode!");
Chris Lattner00950542001-06-06 20:29:01 +000071 unsigned type;
Misha Brukman12c29d12003-09-22 23:38:23 +000072 getTypeSlot(Val->getType(), type);
Chris Lattner1d670cc2001-09-07 16:37:43 +000073 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattner52e20b02003-03-19 20:54:26 +000075 if (ValueTab.size() <= type) {
76 unsigned OldSize = ValueTab.size();
77 ValueTab.resize(type+1);
78 while (OldSize != type+1)
79 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +000080 }
Chris Lattner00950542001-06-06 20:29:01 +000081
82 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000083 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000084 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000085
Chris Lattner52e20b02003-03-19 20:54:26 +000086 bool HasOffset = HasImplicitZeroInitializer &&
Misha Brukman12c29d12003-09-22 23:38:23 +000087 !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +000088
89 return ValueTab[type]->size()-1 + HasOffset;
90}
91
92
93void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
94 Value *Val) {
95 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
96 unsigned type;
Misha Brukman12c29d12003-09-22 23:38:23 +000097 getTypeSlot(Val->getType(), type);
98
Chris Lattner52e20b02003-03-19 20:54:26 +000099 assert((!HasImplicitZeroInitializer || Slot != 0) &&
100 "Cannot change zero init");
101 assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
102 ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000103}
104
105Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
106 unsigned Num = oNum;
107 unsigned type; // The type plane it lives in...
Misha Brukman12c29d12003-09-22 23:38:23 +0000108 getTypeSlot(Ty, type);
Chris Lattner00950542001-06-06 20:29:01 +0000109
Misha Brukman12c29d12003-09-22 23:38:23 +0000110 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000112 if (Num < Type::NumPrimitiveIDs) {
113 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
114 if (T) return (Value*)T; // Asked for a primitive type...
115 }
Chris Lattner00950542001-06-06 20:29:01 +0000116
117 // Otherwise, derived types need offset...
118 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119
Misha Brukman12c29d12003-09-22 23:38:23 +0000120 // Is it a module-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000122 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000123
Misha Brukman12c29d12003-09-22 23:38:23 +0000124 // Nope, is it a function-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000125 Num -= ModuleTypeValues.size();
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000126 if (Num < FunctionTypeValues.size())
127 return (Value*)FunctionTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000128
129 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000130 }
131
Chris Lattner52e20b02003-03-19 20:54:26 +0000132 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
133 if (Num == 0)
134 return Constant::getNullValue(Ty);
135 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000136 }
137
Chris Lattner52e20b02003-03-19 20:54:26 +0000138 if (type < ModuleValues.size()) {
139 if (Num < ModuleValues[type]->size())
140 return ModuleValues[type]->getOperand(Num);
141 Num -= ModuleValues[type]->size();
142 }
143
144 if (Values.size() > type && Values[type]->size() > Num)
145 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000146
Chris Lattner74734132002-08-17 22:01:27 +0000147 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000148
149 Value *d = 0;
150 switch (Ty->getPrimitiveID()) {
Chris Lattner74734132002-08-17 22:01:27 +0000151 case Type::LabelTyID:
152 d = new BBPHolder(Ty, oNum);
153 break;
154 default:
155 d = new ValPHolder(Ty, oNum);
156 break;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158
159 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000160 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000161 return d;
162}
163
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000164/// getConstantValue - Just like getValue, except that it returns a null pointer
165/// only on error. It always returns a constant (meaning that if the value is
166/// defined, but is not a constant, that is an error). If the specified
167/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
168/// after the real value is parsed, the placeholder is eliminated.
169///
170Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
171 if (Value *V = getValue(Ty, Slot, false))
172 return dyn_cast<Constant>(V); // If we already have the value parsed...
173
Chris Lattner52e20b02003-03-19 20:54:26 +0000174 std::pair<const Type*, unsigned> Key(Ty, Slot);
175 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
176
177 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000178 BCR_TRACE(5, "Previous forward ref found!\n");
179 return cast<Constant>(I->second);
180 } else {
181 // Create a placeholder for the constant reference and
182 // keep track of the fact that we have a forward ref to recycle it
183 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
184 Constant *C = new ConstPHolder(Ty, Slot);
185
186 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner52e20b02003-03-19 20:54:26 +0000187 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000188 return C;
189 }
190}
191
192
Misha Brukman12c29d12003-09-22 23:38:23 +0000193void BytecodeParser::postResolveValues(ValueTable &ValTab) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000194 while (!ValTab.empty()) {
Chris Lattner51ca8602003-10-04 19:29:21 +0000195 ValueList &VL = *ValTab.back();
Chris Lattner52e20b02003-03-19 20:54:26 +0000196 ValTab.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattner51ca8602003-10-04 19:29:21 +0000198 while (!VL.empty()) {
199 Value *V = VL.back();
200 unsigned IDNumber = getValueIDNumberFromPlaceHolder(V);
201 VL.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000202
Chris Lattner51ca8602003-10-04 19:29:21 +0000203 Value *NewVal = getValue(V->getType(), IDNumber, false);
204 if (NewVal == 0)
Misha Brukman12c29d12003-09-22 23:38:23 +0000205 throw std::string("Unresolvable reference found: <" +
Chris Lattner51ca8602003-10-04 19:29:21 +0000206 V->getType()->getDescription() + ">:" +
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000207 utostr(IDNumber) + ".");
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner51ca8602003-10-04 19:29:21 +0000209 // Fixup all of the uses of this placeholder def...
210 V->replaceAllUsesWith(NewVal);
211
212 // Now that all the uses are gone, delete the placeholder...
213 // If we couldn't find a def (error case), then leak a little
214 delete V; // memory, 'cause otherwise we can't remove all uses!
Chris Lattner00950542001-06-06 20:29:01 +0000215 }
Chris Lattner51ca8602003-10-04 19:29:21 +0000216 delete &VL;
Chris Lattner00950542001-06-06 20:29:01 +0000217 }
Chris Lattner00950542001-06-06 20:29:01 +0000218}
219
Misha Brukman12c29d12003-09-22 23:38:23 +0000220std::auto_ptr<BasicBlock>
221BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
222 const unsigned char *EndBuf) {
223 std::auto_ptr<BasicBlock> BB(new BasicBlock());
Chris Lattner00950542001-06-06 20:29:01 +0000224
225 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000226 Instruction *Inst;
Misha Brukman12c29d12003-09-22 23:38:23 +0000227 ParseInstruction(Buf, EndBuf, Inst);
228
229 if (Inst == 0) { throw std::string("Could not parse Instruction."); }
230 if (insertValue(Inst, Values) == -1) {
231 throw std::string("Could not insert value.");
Chris Lattner00950542001-06-06 20:29:01 +0000232 }
233
Chris Lattner1d670cc2001-09-07 16:37:43 +0000234 BB->getInstList().push_back(Inst);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000235 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000236 }
237
Misha Brukman12c29d12003-09-22 23:38:23 +0000238 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000239}
240
Misha Brukman12c29d12003-09-22 23:38:23 +0000241void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000242 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000243 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000244 while (Buf < EndBuf) {
245 // Symtab block header: [num entries][type id number]
246 unsigned NumEntries, Typ;
247 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000248 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000249 const Type *Ty = getType(Typ);
Misha Brukman12c29d12003-09-22 23:38:23 +0000250 if (Ty == 0) throw std::string("Invalid type read in symbol table.");
Chris Lattner00950542001-06-06 20:29:01 +0000251
Chris Lattner1d670cc2001-09-07 16:37:43 +0000252 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000253 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000254
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000255 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000256 // Symtab entry: [def slot #][name]
257 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000258 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000259 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000260 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Misha Brukman12c29d12003-09-22 23:38:23 +0000261 throw std::string("Buffer not aligned.");
Chris Lattner00950542001-06-06 20:29:01 +0000262
Chris Lattner52e20b02003-03-19 20:54:26 +0000263 Value *V = getValue(Ty, slot, false); // Find mapping...
264 if (V == 0) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000265 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
266 throw std::string("Failed value look-up.");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000267 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000268 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000269 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000270
Chris Lattner52e20b02003-03-19 20:54:26 +0000271 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000272 }
273 }
274
Misha Brukman12c29d12003-09-22 23:38:23 +0000275 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000276}
277
Chris Lattner74734132002-08-17 22:01:27 +0000278void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000279 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
280 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000281 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000282
Chris Lattner74734132002-08-17 22:01:27 +0000283 BCR_TRACE(3, "Mutating forward refs!\n");
284 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000285
Chris Lattner52e20b02003-03-19 20:54:26 +0000286 VPH->replaceAllUsesWith(NewV);
287
288 // If this is a global variable being resolved, remove the placeholder from
289 // the module...
290 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
291 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000292
Chris Lattner74734132002-08-17 22:01:27 +0000293 delete VPH; // Delete the old placeholder
294 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000295}
296
Misha Brukman12c29d12003-09-22 23:38:23 +0000297void
298BytecodeParser::ParseFunction(const unsigned char *&Buf,
299 const unsigned char *EndBuf) {
300 if (FunctionSignatureList.empty())
301 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000302
Misha Brukman12c29d12003-09-22 23:38:23 +0000303 Function *F = FunctionSignatureList.back().first;
304 unsigned FunctionSlot = FunctionSignatureList.back().second;
305 FunctionSignatureList.pop_back();
306
307 // Save the information for future reading of the function
308 LazyFunctionInfo *LFI = new LazyFunctionInfo();
309 LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
310 LazyFunctionLoadMap[F] = LFI;
311 // Pretend we've `parsed' this function
312 Buf = EndBuf;
313}
314
315void BytecodeParser::materializeFunction(Function* F) {
316 // Find {start, end} pointers and slot in the map. If not there, we're done.
317 std::map<Function*, LazyFunctionInfo*>::iterator Fi =
318 LazyFunctionLoadMap.find(F);
319 if (Fi == LazyFunctionLoadMap.end()) return;
320
321 LazyFunctionInfo *LFI = Fi->second;
322 const unsigned char *Buf = LFI->Buf;
323 const unsigned char *EndBuf = LFI->EndBuf;
324 unsigned FunctionSlot = LFI->FunctionSlot;
325 LazyFunctionLoadMap.erase(Fi);
326 delete LFI;
Chris Lattner00950542001-06-06 20:29:01 +0000327
Chris Lattnere3869c82003-04-16 21:16:05 +0000328 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
329
330 if (!hasInternalMarkerOnly) {
331 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.");
334 if (LinkageType & ~0x3)
335 throw std::string("Invalid linkage type for Function.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000336 Linkage = (GlobalValue::LinkageTypes)LinkageType;
337 } else {
338 // We used to only support two linkage models: internal and external
339 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000340 if (read_vbr(Buf, EndBuf, isInternal))
341 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000342 if (isInternal) Linkage = GlobalValue::InternalLinkage;
343 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000344
Chris Lattnere3869c82003-04-16 21:16:05 +0000345 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000346
Chris Lattner52e20b02003-03-19 20:54:26 +0000347 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
348 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000349 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000350 It != Params.end(); ++It, ++AI) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000351 if (insertValue(AI, Values) == -1)
352 throw std::string("Error reading function arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000353 }
354
355 while (Buf < EndBuf) {
356 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000357 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000358 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000359
360 switch (Type) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000361 case BytecodeFormat::ConstantPool: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000362 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000363 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000364 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000365 }
Chris Lattner00950542001-06-06 20:29:01 +0000366
367 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000368 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000369 std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
370 if (!BB.get() || insertValue(BB.get(), Values) == -1)
371 throw std::string("Parse error: BasicBlock");
Chris Lattner00950542001-06-06 20:29:01 +0000372
Misha Brukman12c29d12003-09-22 23:38:23 +0000373 F->getBasicBlockList().push_back(BB.release());
Chris Lattner00950542001-06-06 20:29:01 +0000374 break;
375 }
376
Misha Brukman12c29d12003-09-22 23:38:23 +0000377 case BytecodeFormat::SymbolTable: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000378 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000379 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000380 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000381 }
Chris Lattner00950542001-06-06 20:29:01 +0000382
383 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000384 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000385 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000386 if (OldBuf > Buf)
387 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000388 break;
389 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000390 BCR_TRACE(2, "} end block\n");
391
Misha Brukman12c29d12003-09-22 23:38:23 +0000392 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000393 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
395
Misha Brukman12c29d12003-09-22 23:38:23 +0000396 // Check for unresolvable references
397 postResolveValues(LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000398
Misha Brukman12c29d12003-09-22 23:38:23 +0000399 //ResolveReferencesToValue(F, FunctionSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000400
Misha Brukman12c29d12003-09-22 23:38:23 +0000401 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000402 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000403
Chris Lattner52e20b02003-03-19 20:54:26 +0000404 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000405}
406
Misha Brukman12c29d12003-09-22 23:38:23 +0000407void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
408 const unsigned char *End) {
409 if (!FunctionSignatureList.empty())
410 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000411
Chris Lattner70cc3392001-09-10 07:58:01 +0000412 // Read global variables...
413 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000414 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000415 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000416 unsigned SlotNo;
417 GlobalValue::LinkageTypes Linkage;
418
419 if (!hasInternalMarkerOnly) {
420 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
421 // bit2,3 = Linkage, bit4+ = slot#
422 SlotNo = VarType >> 4;
423 Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
424 } else {
425 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
426 // bit2 = isInternal, bit3+ = slot#
427 SlotNo = VarType >> 3;
428 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
429 GlobalValue::ExternalLinkage;
430 }
431
432 const Type *Ty = getType(SlotNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000433 if (!Ty || !isa<PointerType>(Ty))
434 throw std::string("Global not pointer type! Ty = " +
435 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000436
Chris Lattner52e20b02003-03-19 20:54:26 +0000437 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000438
Chris Lattner70cc3392001-09-10 07:58:01 +0000439 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000440 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000441 0, "", TheModule);
Chris Lattner05950c32001-10-13 06:47:01 +0000442 int DestSlot = insertValue(GV, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000443 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000444 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000445 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000446
Misha Brukman37f92e22003-09-11 22:34:13 +0000447 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000448 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000449 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000450 GlobalInits.push_back(std::make_pair(GV, InitSlot));
451 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000452 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000453 }
454
Chris Lattner52e20b02003-03-19 20:54:26 +0000455 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000456 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000457 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000458 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
459 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000460 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000461 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000462 throw std::string("Function not ptr to func type! Ty = " +
463 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000464 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000465
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000466 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000467 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000468
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000469 // When the ModuleGlobalInfo section is read, we load the type of each
470 // function and the 'ModuleValues' slot that it lands in. We then load a
471 // placeholder into its slot to reserve it. When the function is loaded,
472 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000473
474 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000475 Function *Func = new Function(cast<FunctionType>(Ty),
476 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000477 int DestSlot = insertValue(Func, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000478 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000479 ResolveReferencesToValue(Func, (unsigned)DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000480
Chris Lattner52e20b02003-03-19 20:54:26 +0000481 // Keep track of this information in a list that is emptied as functions are
482 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000483 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000484 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
485
Misha Brukman12c29d12003-09-22 23:38:23 +0000486 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000487 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000488 }
489
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000490 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000491
492 // Now that the function signature list is set up, reverse it so that we can
493 // remove elements efficiently from the back of the vector.
494 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000495
496 // This is for future proofing... in the future extra fields may be added that
497 // we don't understand, so we transparently ignore them.
498 //
499 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000500}
501
Misha Brukman12c29d12003-09-22 23:38:23 +0000502void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000503 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000504 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000505 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000506
507 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000508 Module::Endianness Endianness;
509 Module::PointerSize PointerSize;
510 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
511 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
512
513 bool hasNoEndianness = Version & 4;
514 bool hasNoPointerSize = Version & 8;
515
516 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000517
518 // Default values for the current bytecode version
Chris Lattner036b8aa2003-03-06 17:55:45 +0000519 HasImplicitZeroInitializer = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000520 hasInternalMarkerOnly = false;
521 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000522
523 switch (RevisionNum) {
524 case 0: // Initial revision
Chris Lattner52e20b02003-03-19 20:54:26 +0000525 // Version #0 didn't have any of the flags stored correctly, and in fact as
526 // only valid with a 14 in the flags values. Also, it does not support
527 // encoding zero initializers for arrays compactly.
528 //
Misha Brukman12c29d12003-09-22 23:38:23 +0000529 if (Version != 14) throw std::string("Unknown revision 0 flags?");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000530 HasImplicitZeroInitializer = false;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000531 Endianness = Module::BigEndian;
532 PointerSize = Module::Pointer64;
Chris Lattnere3869c82003-04-16 21:16:05 +0000533 hasInternalMarkerOnly = true;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000534 hasNoEndianness = hasNoPointerSize = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000535 break;
536 case 1:
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000537 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
538 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000539 hasInternalMarkerOnly = true;
540 break;
541 case 2:
542 // Version #2 added information about all 4 linkage types instead of just
543 // having internal and external.
Chris Lattner036b8aa2003-03-06 17:55:45 +0000544 break;
545 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000546 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000547 }
548
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000549 if (hasNoEndianness) Endianness = Module::AnyEndianness;
550 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000551
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000552 TheModule->setEndianness(Endianness);
553 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000554 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000555 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
556 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000557 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000558}
559
Misha Brukman12c29d12003-09-22 23:38:23 +0000560void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000561 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000562 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000563 readBlock(Buf, EndBuf, Type, Size);
564 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
565 throw std::string("Expected Module packet! B: "+
566 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
567 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000568
Chris Lattner1d670cc2001-09-07 16:37:43 +0000569 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000570 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000571
572 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000573 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000574 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000575
Chris Lattner00950542001-06-06 20:29:01 +0000576 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000577 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000578 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000579 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000580 case BytecodeFormat::GlobalTypePlane:
581 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000582 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000583 break;
584
585 case BytecodeFormat::ModuleGlobalInfo:
586 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000587 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000588 break;
589
Chris Lattner1d670cc2001-09-07 16:37:43 +0000590 case BytecodeFormat::ConstantPool:
591 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000592 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000593 break;
594
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000595 case BytecodeFormat::Function: {
596 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000597 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000598 break;
599 }
600
601 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000602 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000603 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000604 break;
605
606 default:
Chris Lattner00950542001-06-06 20:29:01 +0000607 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000608 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000609 break;
610 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000611 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000612 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000613 }
614
Chris Lattner52e20b02003-03-19 20:54:26 +0000615 // After the module constant pool has been read, we can safely initialize
616 // global variables...
617 while (!GlobalInits.empty()) {
618 GlobalVariable *GV = GlobalInits.back().first;
619 unsigned Slot = GlobalInits.back().second;
620 GlobalInits.pop_back();
621
622 // Look up the initializer value...
623 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000624 if (GV->hasInitializer())
625 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000626 GV->setInitializer(cast<Constant>(V));
627 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000628 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000629 }
630
Misha Brukman12c29d12003-09-22 23:38:23 +0000631 if (!FunctionSignatureList.empty())
632 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000633
634 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000635}
636
Misha Brukman12c29d12003-09-22 23:38:23 +0000637void
638BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
639 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000640
Misha Brukman12c29d12003-09-22 23:38:23 +0000641 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000642
Chris Lattner00950542001-06-06 20:29:01 +0000643 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000644 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000645 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000646 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
647 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000648
Chris Lattner75f20532003-04-22 18:02:52 +0000649 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000650 try {
651 ParseModule(Buf, EndBuf);
652 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000653 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000654 delete TheModule;
655 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000656 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000657 }
Chris Lattner00950542001-06-06 20:29:01 +0000658}