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/Module.h"
17 #include "llvm/Constants.h"
18 #include "llvm/iPHINode.h"
19 #include "llvm/iOther.h"
20 #include <sys/types.h>
27 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
28 if (Ty->isPrimitiveType()) {
29 Slot = Ty->getPrimitiveID();
31 // Check the function level types first...
32 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
33 FunctionTypeValues.end(), Ty);
34 if (I != FunctionTypeValues.end()) {
35 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
36 (&*I - &FunctionTypeValues[0]);
38 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
39 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
40 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
43 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
47 const Type *BytecodeParser::getType(unsigned ID) {
48 if (ID < Type::NumPrimitiveIDs) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
53 //cerr << "Looking up Type ID: " << ID << "\n";
54 const Value *V = getValue(Type::TypeTy, ID, false);
55 return cast_or_null<Type>(V);
58 int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
60 if (getTypeSlot(Val->getType(), type)) return -1;
61 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
63 if (ValueTab.size() <= type)
64 ValueTab.resize(type+1, ValueList());
66 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
67 // << "] = " << Val << "\n";
68 ValueTab[type].push_back(Val);
70 return ValueTab[type].size()-1;
73 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
75 unsigned type; // The type plane it lives in...
77 if (getTypeSlot(Ty, type)) return 0;
79 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
80 assert(Create == false);
81 if (Num < Type::NumPrimitiveIDs) {
82 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
83 if (T) return (Value*)T; // Asked for a primitive type...
86 // Otherwise, derived types need offset...
87 Num -= FirstDerivedTyID;
89 // Is it a module level type?
90 if (Num < ModuleTypeValues.size())
91 return (Value*)ModuleTypeValues[Num].get();
93 // Nope, is it a function level type?
94 Num -= ModuleTypeValues.size();
95 if (Num < FunctionTypeValues.size())
96 return (Value*)FunctionTypeValues[Num].get();
101 if (type < ModuleValues.size()) {
102 if (Num < ModuleValues[type].size())
103 return ModuleValues[type][Num];
104 Num -= ModuleValues[type].size();
107 if (Values.size() > type && Values[type].size() > Num)
108 return Values[type][Num];
110 if (!Create) return 0; // Do not create a placeholder?
113 switch (Ty->getPrimitiveID()) {
114 case Type::FunctionTyID:
115 std::cerr << "Creating function pholder! : " << type << ":" << oNum << " "
116 << Ty->getName() << "\n";
117 d = new FunctionPHolder(Ty, oNum);
118 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
120 case Type::LabelTyID:
121 d = new BBPHolder(Ty, oNum);
124 d = new ValPHolder(Ty, oNum);
128 assert(d != 0 && "How did we not make something?");
129 if (insertValue(d, LateResolveValues) == -1) return 0;
133 /// getConstantValue - Just like getValue, except that it returns a null pointer
134 /// only on error. It always returns a constant (meaning that if the value is
135 /// defined, but is not a constant, that is an error). If the specified
136 /// constant hasn't been parsed yet, a placeholder is defined and used. Later,
137 /// after the real value is parsed, the placeholder is eliminated.
139 Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
140 if (Value *V = getValue(Ty, Slot, false))
141 return dyn_cast<Constant>(V); // If we already have the value parsed...
143 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(Ty, Slot));
144 if (I != GlobalRefs.end()) {
145 BCR_TRACE(5, "Previous forward ref found!\n");
146 return cast<Constant>(I->second);
148 // Create a placeholder for the constant reference and
149 // keep track of the fact that we have a forward ref to recycle it
150 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
151 Constant *C = new ConstPHolder(Ty, Slot);
153 // Keep track of the fact that we have a forward ref to recycle it
154 GlobalRefs.insert(std::make_pair(std::make_pair(Ty, Slot), C));
161 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
163 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
164 ValueList &DL = ValTab[ty];
166 while ((Size = DL.size())) {
167 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
169 Value *D = DL[Size-1];
172 Value *NewDef = getValue(D->getType(), IDNumber, false);
174 Error = true; // Unresolved thinger
175 std::cerr << "Unresolvable reference found: <"
176 << D->getType()->getDescription() << ">:" << IDNumber <<"!\n";
178 // Fixup all of the uses of this placeholder def...
179 D->replaceAllUsesWith(NewDef);
181 // Now that all the uses are gone, delete the placeholder...
182 // If we couldn't find a def (error case), then leak a little
183 delete D; // memory, 'cause otherwise we can't remove all uses!
191 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
193 BB = new BasicBlock();
195 while (Buf < EndBuf) {
197 if (ParseInstruction(Buf, EndBuf, Inst,
203 if (Inst == 0) { delete BB; return true; }
204 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
206 BB->getInstList().push_back(Inst);
214 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
216 while (Buf < EndBuf) {
217 // Symtab block header: [num entries][type id number]
218 unsigned NumEntries, Typ;
219 if (read_vbr(Buf, EndBuf, NumEntries) ||
220 read_vbr(Buf, EndBuf, Typ)) return true;
221 const Type *Ty = getType(Typ);
222 if (Ty == 0) return true;
224 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
227 for (unsigned i = 0; i < NumEntries; ++i) {
228 // Symtab entry: [def slot #][name]
230 if (read_vbr(Buf, EndBuf, slot)) return true;
232 if (read(Buf, EndBuf, Name, false)) // Not aligned...
235 Value *D = getValue(Ty, slot, false); // Find mapping...
237 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
240 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
241 if (!isa<Instruction>(D)) std::cerr << "\n");
243 D->setName(Name, ST);
247 if (Buf > EndBuf) return true;
251 void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
252 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
254 if (I == GlobalRefs.end()) return; // Never forward referenced?
256 BCR_TRACE(3, "Mutating forward refs!\n");
257 Value *VPH = I->second; // Get the placeholder...
259 // Loop over all of the uses of the Value. What they are depends
260 // on what NewV is. Replacing a use of the old reference takes the
261 // use off the use list, so loop with !use_empty(), not the use_iterator.
262 while (!VPH->use_empty()) {
263 Constant *C = cast<Constant>(VPH->use_back());
264 unsigned numReplaced = C->mutateReferences(VPH, NewV);
265 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
267 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
268 // Remove the placeholder GlobalValue from the module...
269 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
273 delete VPH; // Delete the old placeholder
274 GlobalRefs.erase(I); // Remove the map entry for it
277 bool BytecodeParser::ParseFunction(const uchar *&Buf, const uchar *EndBuf) {
278 // Clear out the local values table...
280 if (FunctionSignatureList.empty()) {
281 Error = "Function found, but FunctionSignatureList empty!";
282 return true; // Unexpected function!
285 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
286 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
287 if (MTy == 0) return true; // Not ptr to function!
290 if (read_vbr(Buf, EndBuf, isInternal)) return true;
292 unsigned MethSlot = FunctionSignatureList.back().second;
293 FunctionSignatureList.pop_back();
294 Function *M = new Function(MTy, isInternal != 0);
296 BCR_TRACE(2, "FUNCTION TYPE: " << MTy << "\n");
298 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
299 Function::aiterator AI = M->abegin();
300 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
301 It != Params.end(); ++It, ++AI) {
302 if (insertValue(AI, Values) == -1) {
303 Error = "Error reading function arguments!\n";
304 delete M; return true;
308 while (Buf < EndBuf) {
310 const unsigned char *OldBuf = Buf;
311 if (readBlock(Buf, EndBuf, Type, Size)) {
312 Error = "Error reading Function level block!";
313 delete M; return true;
317 case BytecodeFormat::ConstantPool:
318 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
319 if (ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues)) {
320 delete M; return true;
324 case BytecodeFormat::BasicBlock: {
325 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
327 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
328 insertValue(BB, Values) == -1) {
329 delete M; return true; // Parse error... :(
332 M->getBasicBlockList().push_back(BB);
336 case BytecodeFormat::SymbolTable:
337 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
338 if (ParseSymbolTable(Buf, Buf+Size, &M->getSymbolTable())) {
339 delete M; return true;
344 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
346 if (OldBuf > Buf) return true; // Wrap around!
349 BCR_TRACE(2, "} end block\n");
351 if (align32(Buf, EndBuf)) {
352 Error = "Error aligning Function level block!";
353 delete M; // Malformed bc file, read past end of block.
358 if (postResolveValues(LateResolveValues) ||
359 postResolveValues(LateResolveModuleValues)) {
360 Error = "Error resolving function values!";
361 delete M; return true; // Unresolvable references!
364 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
365 assert(FunctionPHolder && "Something is broken, no placeholder found!");
366 assert(isa<Function>(FunctionPHolder) && "Not a function?");
368 unsigned type; // Type slot
369 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
370 getTypeSlot(PMTy, type);
372 TheModule->getFunctionList().push_back(M);
374 // Replace placeholder with the real function pointer...
375 ModuleValues[type][MethSlot] = M;
377 // Clear out function level types...
378 FunctionTypeValues.clear();
380 // If anyone is using the placeholder make them use the real function instead
381 FunctionPHolder->replaceAllUsesWith(M);
383 // We don't need the placeholder anymore!
384 delete FunctionPHolder;
386 ResolveReferencesToValue(M, MethSlot);
391 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
392 if (!FunctionSignatureList.empty()) {
393 Error = "Two ModuleGlobalInfo packets found!";
394 return true; // Two ModuleGlobal blocks?
397 // Read global variables...
399 if (read_vbr(Buf, End, VarType)) return true;
400 while (VarType != Type::VoidTyID) { // List is terminated by Void
401 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
402 // bit2 = isInternal, bit3+ = slot#
403 const Type *Ty = getType(VarType >> 3);
404 if (!Ty || !isa<PointerType>(Ty)) {
405 Error = "Global not pointer type! Ty = " + Ty->getDescription();
409 const PointerType *PTy = cast<const PointerType>(Ty);
410 const Type *ElTy = PTy->getElementType();
412 Constant *Initializer = 0;
413 if (VarType & 2) { // Does it have an initalizer?
414 // Do not improvise... values must have been stored in the constant pool,
415 // which should have been read before now.
418 if (read_vbr(Buf, End, InitSlot)) return true;
420 Value *V = getValue(ElTy, InitSlot, false);
421 if (V == 0) return true;
422 Initializer = cast<Constant>(V);
425 // Create the global variable...
426 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
428 int DestSlot = insertValue(GV, ModuleValues);
429 if (DestSlot == -1) return true;
431 TheModule->getGlobalList().push_back(GV);
433 ResolveReferencesToValue(GV, (unsigned)DestSlot);
435 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
436 << " into slot #" << DestSlot << "\n");
438 if (read_vbr(Buf, End, VarType)) return true;
441 // Read the function signatures for all of the functions that are coming, and
442 // create fillers in the Value tables.
443 unsigned FnSignature;
444 if (read_vbr(Buf, End, FnSignature)) return true;
445 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
446 const Type *Ty = getType(FnSignature);
447 if (!Ty || !isa<PointerType>(Ty) ||
448 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
449 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
453 // We create functions by passing the underlying FunctionType to create...
454 Ty = cast<PointerType>(Ty)->getElementType();
456 // When the ModuleGlobalInfo section is read, we load the type of each
457 // function and the 'ModuleValues' slot that it lands in. We then load a
458 // placeholder into its slot to reserve it. When the function is loaded,
459 // this placeholder is replaced.
461 // Insert the placeholder...
462 Value *Val = new FunctionPHolder(Ty, 0);
463 if (insertValue(Val, ModuleValues) == -1) return true;
465 // Figure out which entry of its typeslot it went into...
467 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
469 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
471 // Keep track of this information in a linked list that is emptied as
472 // functions are loaded...
474 FunctionSignatureList.push_back(
475 std::make_pair(cast<const PointerType>(Val->getType()), SlotNo));
476 if (read_vbr(Buf, End, FnSignature)) return true;
477 BCR_TRACE(2, "Function of type: " << Ty << "\n");
480 if (align32(Buf, End)) return true;
482 // Now that the function signature list is set up, reverse it so that we can
483 // remove elements efficiently from the back of the vector.
484 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
486 // This is for future proofing... in the future extra fields may be added that
487 // we don't understand, so we transparently ignore them.
493 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf) {
495 if (readBlock(Buf, EndBuf, Type, Size)) return true;
496 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
497 Error = "Expected Module packet!";
498 return true; // Hrm, not a class?
501 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
502 FunctionSignatureList.clear(); // Just in case...
504 // Read into instance variables...
505 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
506 if (align32(Buf, EndBuf)) return true;
507 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
509 while (Buf < EndBuf) {
510 const unsigned char *OldBuf = Buf;
511 if (readBlock(Buf, EndBuf, Type, Size)) return true;
513 case BytecodeFormat::ConstantPool:
514 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
515 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues))
519 case BytecodeFormat::ModuleGlobalInfo:
520 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
521 if (ParseModuleGlobalInfo(Buf, Buf+Size)) return true;
524 case BytecodeFormat::Function: {
525 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
526 if (ParseFunction(Buf, Buf+Size)) return true; // Error parsing function
530 case BytecodeFormat::SymbolTable:
531 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
532 if (ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()))
537 Error = "Expected Module Block!";
539 if (OldBuf > Buf) return true; // Wrap around!
542 BCR_TRACE(1, "} end block\n");
543 if (align32(Buf, EndBuf)) return true;
546 if (!FunctionSignatureList.empty()) { // Expected more functions!
547 Error = "Function expected, but bytecode stream at end!";
551 BCR_TRACE(0, "} end block\n\n");
555 static inline Module *Error(std::string *ErrorStr, const char *Message) {
556 if (ErrorStr) *ErrorStr = Message;
560 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
561 LateResolveValues.clear();
563 // Read and check signature...
564 if (read(Buf, EndBuf, Sig) ||
565 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
566 return ::Error(&Error, "Invalid bytecode signature!");
568 TheModule = new Module();
569 if (ParseModule(Buf, EndBuf)) {
577 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
578 std::string *ErrorStr) {
579 BytecodeParser Parser;
580 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length);
581 if (ErrorStr) *ErrorStr = Parser.getError();
586 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
587 /// when the object is destroyed.
591 FDHandle(int fd) : FD(fd) {}
592 operator int() const { return FD; }
594 if (FD != -1) close(FD);
598 // Parse and return a class file...
600 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
603 if (Filename != std::string("-")) { // Read from a file...
604 FDHandle FD = open(Filename.c_str(), O_RDONLY);
606 return Error(ErrorStr, "Error opening file!");
608 // Stat the file to get its length...
610 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
611 return Error(ErrorStr, "Error stat'ing file!");
613 // mmap in the file all at once...
614 int Length = StatBuf.st_size;
615 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
617 if (Buffer == (unsigned char*)MAP_FAILED)
618 return Error(ErrorStr, "Error mmapping file!");
620 // Parse the bytecode we mmapped in
621 Result = ParseBytecodeBuffer(Buffer, Length, ErrorStr);
623 // Unmmap the bytecode...
624 munmap((char*)Buffer, Length);
625 } else { // Read from stdin
627 uchar Buffer[4096*4];
628 std::vector<unsigned char> FileData;
630 // Read in all of the data from stdin, we cannot mmap stdin...
631 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
633 return Error(ErrorStr, "Error reading from stdin!");
635 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
638 if (FileData.empty())
639 return Error(ErrorStr, "Standard Input empty!");
643 uchar *Buf = (uchar*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
644 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
645 assert((Buf != (uchar*)-1) && "mmap returned error!");
646 memcpy(Buf, &FileData[0], FileData.size());
648 unsigned char *Buf = &FileData[0];
651 Result = ParseBytecodeBuffer(Buf, FileData.size(), ErrorStr);
654 munmap((char*)Buf, FileData.size()); // Free mmap'd data area