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: Make error message outputs be configurable depending on an option?
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/GlobalVariable.h"
17 #include "llvm/Module.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/ConstantVals.h"
20 #include "llvm/iPHINode.h"
21 #include "llvm/iOther.h"
22 #include "llvm/Argument.h"
23 #include <sys/types.h>
33 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
34 if (Ty->isPrimitiveType()) {
35 Slot = Ty->getPrimitiveID();
37 // Check the method level types first...
38 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
39 MethodTypeValues.end(), Ty);
40 if (I != MethodTypeValues.end()) {
41 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
42 (&*I - &MethodTypeValues[0]);
44 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
45 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
46 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
49 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
53 const Type *BytecodeParser::getType(unsigned ID) {
54 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
57 //cerr << "Looking up Type ID: " << ID << "\n";
59 const Value *D = getValue(Type::TypeTy, ID, false);
60 if (D == 0) return failure<const Type*>(0);
65 int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
67 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
68 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
70 if (ValueTab.size() <= type)
71 ValueTab.resize(type+1, ValueList());
73 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
74 // << "] = " << Val << "\n";
75 ValueTab[type].push_back(Val);
77 return ValueTab[type].size()-1;
80 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
82 unsigned type; // The type plane it lives in...
84 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
86 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
87 assert(Create == false);
88 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
89 if (T) return (Value*)T; // Asked for a primitive type...
91 // Otherwise, derived types need offset...
92 Num -= FirstDerivedTyID;
94 // Is it a module level type?
95 if (Num < ModuleTypeValues.size())
96 return (Value*)ModuleTypeValues[Num].get();
98 // Nope, is it a method level type?
99 Num -= ModuleTypeValues.size();
100 if (Num < MethodTypeValues.size())
101 return (Value*)MethodTypeValues[Num].get();
106 if (type < ModuleValues.size()) {
107 if (Num < ModuleValues[type].size())
108 return ModuleValues[type][Num];
109 Num -= ModuleValues[type].size();
112 if (Values.size() > type && Values[type].size() > Num)
113 return Values[type][Num];
115 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
118 switch (Ty->getPrimitiveID()) {
119 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
120 case Type::FunctionTyID:
121 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
122 << Ty->getName() << "\n";
123 d = new MethPHolder(Ty, oNum);
124 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
126 default: d = new DefPHolder(Ty, oNum); break;
129 assert(d != 0 && "How did we not make something?");
130 if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
134 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
136 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
137 ValueList &DL = ValTab[ty];
139 while ((Size = DL.size())) {
140 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
142 Value *D = DL[Size-1];
145 Value *NewDef = getValue(D->getType(), IDNumber, false);
147 Error = true; // Unresolved thinger
148 cerr << "Unresolvable reference found: <"
149 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
151 // Fixup all of the uses of this placeholder def...
152 D->replaceAllUsesWith(NewDef);
154 // Now that all the uses are gone, delete the placeholder...
155 // If we couldn't find a def (error case), then leak a little
156 delete D; // memory, 'cause otherwise we can't remove all uses!
164 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
166 BB = new BasicBlock();
168 while (Buf < EndBuf) {
170 if (ParseInstruction(Buf, EndBuf, Inst)) {
172 return failure(true);
175 if (Inst == 0) { delete BB; return failure(true); }
176 if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
178 BB->getInstList().push_back(Inst);
186 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
188 while (Buf < EndBuf) {
189 // Symtab block header: [num entries][type id number]
190 unsigned NumEntries, Typ;
191 if (read_vbr(Buf, EndBuf, NumEntries) ||
192 read_vbr(Buf, EndBuf, Typ)) return failure(true);
193 const Type *Ty = getType(Typ);
194 if (Ty == 0) return failure(true);
196 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
199 for (unsigned i = 0; i < NumEntries; ++i) {
200 // Symtab entry: [def slot #][name]
202 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
204 if (read(Buf, EndBuf, Name, false)) // Not aligned...
205 return failure(true);
207 Value *D = getValue(Ty, slot, false); // Find mapping...
209 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
210 return failure(true);
212 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
213 if (!isa<Instruction>(D)) cerr << "\n");
215 D->setName(Name, ST);
219 if (Buf > EndBuf) return failure(true);
223 // DeclareNewGlobalValue - Patch up forward references to global values in the
224 // form of ConstantPointerRef.
226 void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
227 // Check to see if there is a forward reference to this global variable...
228 // if there is, eliminate it and patch the reference to use the new def'n.
229 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
231 if (I != GlobalRefs.end()) {
232 GlobalVariable *OldGV = I->second; // Get the placeholder...
233 BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
235 // Loop over all of the uses of the GlobalValue. The only thing they are
236 // allowed to be at this point is ConstantPointerRef's.
237 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
238 while (!OldGV->use_empty()) {
239 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
240 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
241 assert(CPPR->getValue() == OldGV && "Something isn't happy");
243 BCR_TRACE(4, "Mutating Forward Ref!\n");
245 // Change the const pool reference to point to the real global variable
246 // now. This should drop a use from the OldGV.
247 CPPR->mutateReference(GV);
250 // Remove GV from the module...
251 GV->getParent()->getGlobalList().remove(OldGV);
252 delete OldGV; // Delete the old placeholder
254 // Remove the map entry for the global now that it has been created...
259 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
261 // Clear out the local values table...
263 if (MethodSignatureList.empty()) {
264 Error = "Function found, but FunctionSignatureList empty!";
265 return failure(true); // Unexpected method!
268 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
269 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
270 if (MTy == 0) return failure(true); // Not ptr to method!
273 if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
275 unsigned MethSlot = MethodSignatureList.front().second;
276 MethodSignatureList.pop_front();
277 Function *M = new Function(MTy, isInternal != 0);
279 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
281 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
282 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
283 It != Params.end(); ++It) {
284 Argument *FA = new Argument(*It);
285 if (insertValue(FA, Values) == -1) {
286 Error = "Error reading method arguments!\n";
287 delete M; return failure(true);
289 M->getArgumentList().push_back(FA);
292 while (Buf < EndBuf) {
294 const uchar *OldBuf = Buf;
295 if (readBlock(Buf, EndBuf, Type, Size)) {
296 Error = "Error reading Function level block!";
297 delete M; return failure(true);
301 case BytecodeFormat::ConstantPool:
302 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
303 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
304 delete M; return failure(true);
308 case BytecodeFormat::BasicBlock: {
309 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
311 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
312 insertValue(BB, Values) == -1) {
313 delete M; return failure(true); // Parse error... :(
316 M->getBasicBlocks().push_back(BB);
320 case BytecodeFormat::SymbolTable:
321 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
322 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
323 delete M; return failure(true);
328 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
330 if (OldBuf > Buf) return failure(true); // Wrap around!
333 BCR_TRACE(2, "} end block\n");
335 if (align32(Buf, EndBuf)) {
336 Error = "Error aligning Function level block!";
337 delete M; // Malformed bc file, read past end of block.
338 return failure(true);
342 if (postResolveValues(LateResolveValues) ||
343 postResolveValues(LateResolveModuleValues)) {
344 Error = "Error resolving method values!";
345 delete M; return failure(true); // Unresolvable references!
348 Value *MethPHolder = getValue(PMTy, MethSlot, false);
349 assert(MethPHolder && "Something is broken no placeholder found!");
350 assert(isa<Function>(MethPHolder) && "Not a function?");
352 unsigned type; // Type slot
353 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
354 getTypeSlot(PMTy, type);
356 C->getFunctionList().push_back(M);
358 // Replace placeholder with the real method pointer...
359 ModuleValues[type][MethSlot] = M;
361 // Clear out method level types...
362 MethodTypeValues.clear();
364 // If anyone is using the placeholder make them use the real method instead
365 MethPHolder->replaceAllUsesWith(M);
367 // We don't need the placeholder anymore!
370 // If the method is empty, we don't need the method argument entries...
372 M->getArgumentList().delete_all();
374 DeclareNewGlobalValue(M, MethSlot);
379 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
381 if (!MethodSignatureList.empty()) {
382 Error = "Two ModuleGlobalInfo packets found!";
383 return failure(true); // Two ModuleGlobal blocks?
386 // Read global variables...
388 if (read_vbr(Buf, End, VarType)) return failure(true);
389 while (VarType != Type::VoidTyID) { // List is terminated by Void
390 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
391 // bit2 = isInternal, bit3+ = slot#
392 const Type *Ty = getType(VarType >> 3);
393 if (!Ty || !Ty->isPointerType()) {
394 Error = "Global not pointer type! Ty = " + Ty->getDescription();
395 return failure(true);
398 const PointerType *PTy = cast<const PointerType>(Ty);
399 const Type *ElTy = PTy->getElementType();
401 Constant *Initializer = 0;
402 if (VarType & 2) { // Does it have an initalizer?
403 // Do not improvise... values must have been stored in the constant pool,
404 // which should have been read before now.
407 if (read_vbr(Buf, End, InitSlot)) return failure(true);
409 Value *V = getValue(ElTy, InitSlot, false);
410 if (V == 0) return failure(true);
411 Initializer = cast<Constant>(V);
414 // Create the global variable...
415 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
417 int DestSlot = insertValue(GV, ModuleValues);
418 if (DestSlot == -1) return failure(true);
420 Mod->getGlobalList().push_back(GV);
422 DeclareNewGlobalValue(GV, unsigned(DestSlot));
424 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
425 << " into slot #" << DestSlot << "\n");
427 if (read_vbr(Buf, End, VarType)) return failure(true);
430 // Read the method signatures for all of the methods that are coming, and
431 // create fillers in the Value tables.
432 unsigned MethSignature;
433 if (read_vbr(Buf, End, MethSignature)) return failure(true);
434 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
435 const Type *Ty = getType(MethSignature);
436 if (!Ty || !isa<PointerType>(Ty) ||
437 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
438 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
439 return failure(true);
442 // We create methods by passing the underlying FunctionType to create...
443 Ty = cast<PointerType>(Ty)->getElementType();
445 // When the ModuleGlobalInfo section is read, we load the type of each
446 // method and the 'ModuleValues' slot that it lands in. We then load a
447 // placeholder into its slot to reserve it. When the method is loaded, this
448 // placeholder is replaced.
450 // Insert the placeholder...
451 Value *Val = new MethPHolder(Ty, 0);
452 if (insertValue(Val, ModuleValues) == -1) return failure(true);
454 // Figure out which entry of its typeslot it went into...
456 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
458 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
460 // Keep track of this information in a linked list that is emptied as
461 // methods are loaded...
463 MethodSignatureList.push_back(
464 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
465 if (read_vbr(Buf, End, MethSignature)) return failure(true);
466 BCR_TRACE(2, "Function of type: " << Ty << "\n");
469 if (align32(Buf, End)) return failure(true);
471 // This is for future proofing... in the future extra fields may be added that
472 // we don't understand, so we transparently ignore them.
478 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
482 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
483 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
484 Error = "Expected Module packet!";
485 return failure(true); // Hrm, not a class?
488 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
489 MethodSignatureList.clear(); // Just in case...
491 // Read into instance variables...
492 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
493 if (align32(Buf, EndBuf)) return failure(true);
494 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
496 TheModule = C = new Module();
497 while (Buf < EndBuf) {
498 const uchar *OldBuf = Buf;
499 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
501 case BytecodeFormat::ConstantPool:
502 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
503 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
504 delete C; return failure(true);
508 case BytecodeFormat::ModuleGlobalInfo:
509 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
511 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
512 delete C; return failure(true);
516 case BytecodeFormat::Function: {
517 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
518 if (ParseMethod(Buf, Buf+Size, C)) {
519 delete C; return failure(true); // Error parsing method
524 case BytecodeFormat::SymbolTable:
525 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
526 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
527 delete C; return failure(true);
532 Error = "Expected Module Block!";
534 if (OldBuf > Buf) return failure(true); // Wrap around!
537 BCR_TRACE(1, "} end block\n");
538 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
541 if (!MethodSignatureList.empty()) { // Expected more methods!
542 Error = "Function expected, but bytecode stream at end!";
543 return failure(true);
546 BCR_TRACE(0, "} end block\n\n");
550 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
551 LateResolveValues.clear();
553 // Read and check signature...
554 if (read(Buf, EndBuf, Sig) ||
555 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
556 Error = "Invalid bytecode signature!";
557 return failure<Module*>(0); // Invalid signature!
561 if (ParseModule(Buf, EndBuf, Result)) return 0;
566 Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
567 BytecodeParser Parser;
568 return Parser.ParseBytecode(Buffer, Buffer+Length);
571 // Parse and return a class file...
573 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
577 if (Filename != std::string("-")) { // Read from a file...
578 int FD = open(Filename.c_str(), O_RDONLY);
580 if (ErrorStr) *ErrorStr = "Error opening file!";
581 return failure<Module*>(0);
584 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
586 int Length = StatBuf.st_size;
588 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
589 close(FD); return failure<Module*>(0);
591 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
593 if (Buffer == (uchar*)-1) {
594 if (ErrorStr) *ErrorStr = "Error mmapping file!";
595 close(FD); return failure<Module*>(0);
598 BytecodeParser Parser;
599 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
601 munmap((char*)Buffer, Length);
603 if (ErrorStr) *ErrorStr = Parser.getError();
604 } else { // Read from stdin
607 uchar Buffer[4096], *FileData = 0;
608 while ((BlockSize = read(0, Buffer, 4))) {
609 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
611 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
612 memcpy(FileData+FileSize, Buffer, BlockSize);
613 FileSize += BlockSize;
617 if (ErrorStr) *ErrorStr = "Standard Input empty!";
618 free(FileData); return failure<Module*>(0);
623 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
624 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
625 assert((Buf != (uchar*)-1) && "mmap returned error!");
626 memcpy(Buf, FileData, FileSize);
629 uchar *Buf = FileData;
632 BytecodeParser Parser;
633 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
636 munmap((char*)Buf, FileSize); // Free mmap'd data area
638 free(FileData); // Free realloc'd block of memory
641 if (ErrorStr) *ErrorStr = Parser.getError();