1 //===- Reader.cpp - Code to read bytecode files ---------------------------===//
3 // This library implements the functionality defined in llvm/Bytecode/Reader.h
5 // Note that this library should be as fast as possible, reentrant, and
8 // TODO: Return error messages to caller instead of printing them out directly.
9 // TODO: Allow passing in an option to ignore the symbol table
11 //===----------------------------------------------------------------------===//
13 #include "ReaderInternals.h"
14 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Bytecode/Format.h"
16 #include "llvm/Constants.h"
17 #include "llvm/iPHINode.h"
18 #include "llvm/iOther.h"
19 #include "llvm/Module.h"
20 #include "Support/StringExtras.h"
21 #include "Config/unistd.h"
22 #include "Config/sys/mman.h"
23 #include "Config/sys/stat.h"
24 #include "Config/sys/types.h"
28 #define CHECK_ALIGN32(begin,end) \
29 if (align32(begin,end)) \
30 throw std::string("Alignment error: Reader.cpp:" + \
31 utostr((unsigned)__LINE__));
34 BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
35 if (Ty->isPrimitiveType()) {
36 Slot = Ty->getPrimitiveID();
38 // Check the function level types first...
39 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
40 FunctionTypeValues.end(), Ty);
41 if (I != FunctionTypeValues.end()) {
42 Slot = FirstDerivedTyID + ModuleTypeValues.size() +
43 (&*I - &FunctionTypeValues[0]);
45 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
46 if (I == ModuleTypeValues.end())
47 throw std::string("Didn't find type in ModuleTypeValues.");
48 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
51 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
54 const Type *BytecodeParser::getType(unsigned ID) {
55 if (ID < Type::NumPrimitiveIDs) {
56 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
60 //cerr << "Looking up Type ID: " << ID << "\n";
61 const Value *V = getValue(Type::TypeTy, ID, false);
62 return cast_or_null<Type>(V);
65 int 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!");
71 getTypeSlot(Val->getType(), type);
72 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
74 if (ValueTab.size() <= type) {
75 unsigned OldSize = ValueTab.size();
76 ValueTab.resize(type+1);
77 while (OldSize != type+1)
78 ValueTab[OldSize++] = new ValueList();
81 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
82 // << "] = " << Val << "\n";
83 ValueTab[type]->push_back(Val);
85 bool HasOffset = HasImplicitZeroInitializer &&
86 !Val->getType()->isPrimitiveType();
88 return ValueTab[type]->size()-1 + HasOffset;
92 void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
94 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
96 getTypeSlot(Val->getType(), type);
98 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);
104 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
106 unsigned type; // The type plane it lives in...
107 getTypeSlot(Ty, type);
109 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
110 assert(Create == false);
111 if (Num < Type::NumPrimitiveIDs) {
112 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
113 if (T) return (Value*)T; // Asked for a primitive type...
116 // Otherwise, derived types need offset...
117 Num -= FirstDerivedTyID;
119 // Is it a module-level type?
120 if (Num < ModuleTypeValues.size())
121 return (Value*)ModuleTypeValues[Num].get();
123 // Nope, is it a function-level type?
124 Num -= ModuleTypeValues.size();
125 if (Num < FunctionTypeValues.size())
126 return (Value*)FunctionTypeValues[Num].get();
131 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
133 return Constant::getNullValue(Ty);
137 if (type < ModuleValues.size()) {
138 if (Num < ModuleValues[type]->size())
139 return ModuleValues[type]->getOperand(Num);
140 Num -= ModuleValues[type]->size();
143 if (Values.size() > type && Values[type]->size() > Num)
144 return Values[type]->getOperand(Num);
146 if (!Create) return 0; // Do not create a placeholder?
149 switch (Ty->getPrimitiveID()) {
150 case Type::LabelTyID:
151 d = new BBPHolder(Ty, oNum);
154 d = new ValPHolder(Ty, oNum);
158 assert(d != 0 && "How did we not make something?");
159 if (insertValue(d, LateResolveValues) == -1) return 0;
163 /// 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.
169 Constant *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...
173 std::pair<const Type*, unsigned> Key(Ty, Slot);
174 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
176 if (I != GlobalRefs.end() && I->first == Key) {
177 BCR_TRACE(5, "Previous forward ref found!\n");
178 return cast<Constant>(I->second);
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);
185 // Keep track of the fact that we have a forward ref to recycle it
186 GlobalRefs.insert(I, std::make_pair(Key, C));
192 void BytecodeParser::postResolveValues(ValueTable &ValTab) {
193 while (!ValTab.empty()) {
194 ValueList &DL = *ValTab.back();
197 while (!DL.empty()) {
198 Value *D = DL.back();
199 unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
202 Value *NewDef = getValue(D->getType(), IDNumber, false);
204 throw std::string("Unresolvable reference found: <" +
205 D->getType()->getName() + ">:" +utostr(IDNumber)+".");
207 // Fixup all of the uses of this placeholder def...
208 D->replaceAllUsesWith(NewDef);
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
212 delete D; // memory, 'cause otherwise we can't remove all uses!
219 std::auto_ptr<BasicBlock>
220 BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
221 const unsigned char *EndBuf) {
222 std::auto_ptr<BasicBlock> BB(new BasicBlock());
224 while (Buf < EndBuf) {
226 ParseInstruction(Buf, EndBuf, Inst);
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.");
233 BB->getInstList().push_back(Inst);
240 void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
241 const unsigned char *EndBuf,
243 while (Buf < EndBuf) {
244 // Symtab block header: [num entries][type id number]
245 unsigned NumEntries, Typ;
246 if (read_vbr(Buf, EndBuf, NumEntries) ||
247 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
248 const Type *Ty = getType(Typ);
249 if (Ty == 0) throw std::string("Invalid type read in symbol table.");
251 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
254 for (unsigned i = 0; i < NumEntries; ++i) {
255 // Symtab entry: [def slot #][name]
257 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
259 if (read(Buf, EndBuf, Name, false)) // Not aligned...
260 throw std::string("Buffer not aligned.");
262 Value *V = getValue(Ty, slot, false); // Find mapping...
264 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
265 throw std::string("Failed value look-up.");
267 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
268 if (!isa<Instruction>(V)) std::cerr << "\n");
270 V->setName(Name, ST);
274 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
277 void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
278 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
280 if (I == GlobalRefs.end()) return; // Never forward referenced?
282 BCR_TRACE(3, "Mutating forward refs!\n");
283 Value *VPH = I->second; // Get the placeholder...
285 VPH->replaceAllUsesWith(NewV);
287 // If this is a global variable being resolved, remove the placeholder from
289 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
290 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
292 delete VPH; // Delete the old placeholder
293 GlobalRefs.erase(I); // Remove the map entry for it
297 BytecodeParser::ParseFunction(const unsigned char *&Buf,
298 const unsigned char *EndBuf) {
299 if (FunctionSignatureList.empty())
300 throw std::string("FunctionSignatureList empty!");
302 Function *F = FunctionSignatureList.back().first;
303 unsigned FunctionSlot = FunctionSignatureList.back().second;
304 FunctionSignatureList.pop_back();
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
314 void 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;
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);
327 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
329 if (!hasInternalMarkerOnly) {
330 unsigned LinkageType;
331 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.");
335 Linkage = (GlobalValue::LinkageTypes)LinkageType;
337 // We used to only support two linkage models: internal and external
339 if (read_vbr(Buf, EndBuf, isInternal))
340 throw std::string("ParseFunction: Error reading from buffer.");
341 if (isInternal) Linkage = GlobalValue::InternalLinkage;
344 F->setLinkage(Linkage);
346 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
347 Function::aiterator AI = F->abegin();
348 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
349 It != Params.end(); ++It, ++AI) {
350 if (insertValue(AI, Values) == -1)
351 throw std::string("Error reading function arguments!");
354 while (Buf < EndBuf) {
356 const unsigned char *OldBuf = Buf;
357 readBlock(Buf, EndBuf, Type, Size);
360 case BytecodeFormat::ConstantPool: {
361 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
362 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
366 case BytecodeFormat::BasicBlock: {
367 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
368 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");
372 F->getBasicBlockList().push_back(BB.release());
376 case BytecodeFormat::SymbolTable: {
377 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
378 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
383 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
386 throw std::string("Wrapped around reading bytecode.");
389 BCR_TRACE(2, "} end block\n");
391 // Malformed bc file if read past end of block.
392 CHECK_ALIGN32(Buf, EndBuf);
395 // Check for unresolvable references
396 postResolveValues(LateResolveValues);
398 //ResolveReferencesToValue(F, FunctionSlot);
400 // Clear out function-level types...
401 FunctionTypeValues.clear();
406 void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
407 const unsigned char *End) {
408 if (!FunctionSignatureList.empty())
409 throw std::string("Two ModuleGlobalInfo packets found!");
411 // Read global variables...
413 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
414 while (VarType != Type::VoidTyID) { // List is terminated by Void
416 GlobalValue::LinkageTypes Linkage;
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);
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;
431 const Type *Ty = getType(SlotNo);
432 if (!Ty || !isa<PointerType>(Ty))
433 throw std::string("Global not pointer type! Ty = " +
434 Ty->getDescription());
436 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
438 // Create the global variable...
439 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
441 int DestSlot = insertValue(GV, ModuleValues);
442 if (DestSlot == -1) throw Error_DestSlot;
443 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
444 ResolveReferencesToValue(GV, (unsigned)DestSlot);
446 if (VarType & 2) { // Does it have an initializer?
448 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
449 GlobalInits.push_back(std::make_pair(GV, InitSlot));
451 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
454 // Read the function objects for all of the functions that are coming
455 unsigned FnSignature;
456 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
457 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
458 const Type *Ty = getType(FnSignature);
459 if (!Ty || !isa<PointerType>(Ty) ||
460 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
461 throw std::string("Function not ptr to func type! Ty = " +
462 Ty->getDescription());
465 // We create functions by passing the underlying FunctionType to create...
466 Ty = cast<PointerType>(Ty)->getElementType();
468 // 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.
473 // Insert the placeholder...
474 Function *Func = new Function(cast<FunctionType>(Ty),
475 GlobalValue::InternalLinkage, "", TheModule);
476 int DestSlot = insertValue(Func, ModuleValues);
477 if (DestSlot == -1) throw Error_DestSlot;
478 ResolveReferencesToValue(Func, (unsigned)DestSlot);
480 // Keep track of this information in a list that is emptied as functions are
483 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
485 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
486 BCR_TRACE(2, "Function of type: " << Ty << "\n");
489 CHECK_ALIGN32(Buf, End);
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());
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.
501 void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
502 const unsigned char *EndBuf) {
504 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
506 // Unpack version number: low four bits are for flags, top bits = version
507 Module::Endianness Endianness;
508 Module::PointerSize PointerSize;
509 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
510 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
512 bool hasNoEndianness = Version & 4;
513 bool hasNoPointerSize = Version & 8;
515 RevisionNum = Version >> 4;
517 // Default values for the current bytecode version
518 HasImplicitZeroInitializer = true;
519 hasInternalMarkerOnly = false;
520 FirstDerivedTyID = 14;
522 switch (RevisionNum) {
523 case 0: // Initial revision
524 // 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.
528 if (Version != 14) throw std::string("Unknown revision 0 flags?");
529 HasImplicitZeroInitializer = false;
530 Endianness = Module::BigEndian;
531 PointerSize = Module::Pointer64;
532 hasInternalMarkerOnly = true;
533 hasNoEndianness = hasNoPointerSize = false;
536 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
537 // hasNoEndianness, and hasNoPointerSize.
538 hasInternalMarkerOnly = true;
541 // Version #2 added information about all 4 linkage types instead of just
542 // having internal and external.
545 throw std::string("Unknown bytecode version number!");
548 if (hasNoEndianness) Endianness = Module::AnyEndianness;
549 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
551 TheModule->setEndianness(Endianness);
552 TheModule->setPointerSize(PointerSize);
553 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
554 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
555 << PointerSize << "\n");
556 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
559 void BytecodeParser::ParseModule(const unsigned char *Buf,
560 const unsigned char *EndBuf) {
562 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?
568 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
569 FunctionSignatureList.clear(); // Just in case...
571 // Read into instance variables...
572 ParseVersionInfo(Buf, EndBuf);
573 CHECK_ALIGN32(Buf, EndBuf);
575 while (Buf < EndBuf) {
576 const unsigned char *OldBuf = Buf;
577 readBlock(Buf, EndBuf, Type, Size);
579 case BytecodeFormat::GlobalTypePlane:
580 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
581 ParseGlobalTypes(Buf, Buf+Size);
584 case BytecodeFormat::ModuleGlobalInfo:
585 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
586 ParseModuleGlobalInfo(Buf, Buf+Size);
589 case BytecodeFormat::ConstantPool:
590 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
591 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
594 case BytecodeFormat::Function: {
595 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
596 ParseFunction(Buf, Buf+Size);
600 case BytecodeFormat::SymbolTable:
601 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
602 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
607 if (OldBuf > Buf) throw std::string("Expected Module Block!");
610 BCR_TRACE(1, "} end block\n");
611 CHECK_ALIGN32(Buf, EndBuf);
614 // 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();
621 // Look up the initializer value...
622 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
623 if (GV->hasInitializer())
624 throw std::string("Global *already* has an initializer?!");
625 GV->setInitializer(cast<Constant>(V));
627 throw std::string("Cannot find initializer value.");
630 if (!FunctionSignatureList.empty())
631 throw std::string("Function expected, but bytecode stream ended!");
633 BCR_TRACE(0, "} end block\n\n");
637 BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
638 const std::string &ModuleID) {
640 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
641 // Read and check signature...
642 if (read(Buf, EndBuf, Sig) ||
643 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
644 throw std::string("Invalid bytecode signature!");
646 TheModule = new Module(ModuleID);
648 ParseModule(Buf, EndBuf);
649 } catch (std::string &Error) {
650 freeState(); // Must destroy handles before deleting module!