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>
30 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
31 if (Ty->isPrimitiveType()) {
32 Slot = Ty->getPrimitiveID();
34 // Check the method level types first...
35 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
36 MethodTypeValues.end(), Ty);
37 if (I != MethodTypeValues.end()) {
38 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
39 (&*I - &MethodTypeValues[0]);
41 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
42 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
43 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
46 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
50 const Type *BytecodeParser::getType(unsigned ID) {
51 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
54 //cerr << "Looking up Type ID: " << ID << "\n";
56 const Value *D = getValue(Type::TypeTy, ID, false);
62 int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
64 if (getTypeSlot(Val->getType(), type)) return -1;
65 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
67 if (ValueTab.size() <= type)
68 ValueTab.resize(type+1, ValueList());
70 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
71 // << "] = " << Val << "\n";
72 ValueTab[type].push_back(Val);
74 return ValueTab[type].size()-1;
77 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
79 unsigned type; // The type plane it lives in...
81 if (getTypeSlot(Ty, type)) return 0;
83 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
84 assert(Create == false);
85 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
86 if (T) return (Value*)T; // Asked for a primitive type...
88 // Otherwise, derived types need offset...
89 Num -= FirstDerivedTyID;
91 // Is it a module level type?
92 if (Num < ModuleTypeValues.size())
93 return (Value*)ModuleTypeValues[Num].get();
95 // Nope, is it a method level type?
96 Num -= ModuleTypeValues.size();
97 if (Num < MethodTypeValues.size())
98 return (Value*)MethodTypeValues[Num].get();
103 if (type < ModuleValues.size()) {
104 if (Num < ModuleValues[type].size())
105 return ModuleValues[type][Num];
106 Num -= ModuleValues[type].size();
109 if (Values.size() > type && Values[type].size() > Num)
110 return Values[type][Num];
112 if (!Create) return 0; // Do not create a placeholder?
115 switch (Ty->getPrimitiveID()) {
116 case Type::FunctionTyID:
117 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
118 << Ty->getName() << "\n";
119 d = new FunctionPHolder(Ty, oNum);
120 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
122 case Type::LabelTyID:
123 d = new BBPHolder(Ty, oNum);
126 d = new ValPHolder(Ty, oNum);
130 assert(d != 0 && "How did we not make something?");
131 if (insertValue(d, LateResolveValues) == -1) return 0;
135 /// getConstantValue - Just like getValue, except that it returns a null pointer
136 /// only on error. It always returns a constant (meaning that if the value is
137 /// defined, but is not a constant, that is an error). If the specified
138 /// constant hasn't been parsed yet, a placeholder is defined and used. Later,
139 /// after the real value is parsed, the placeholder is eliminated.
141 Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
142 if (Value *V = getValue(Ty, Slot, false))
143 return dyn_cast<Constant>(V); // If we already have the value parsed...
145 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot));
146 if (I != GlobalRefs.end()) {
147 BCR_TRACE(5, "Previous forward ref found!\n");
148 return cast<Constant>(I->second);
150 // Create a placeholder for the constant reference and
151 // keep track of the fact that we have a forward ref to recycle it
152 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
153 Constant *C = new ConstPHolder(Ty, Slot);
155 // Keep track of the fact that we have a forward ref to recycle it
156 GlobalRefs.insert(make_pair(make_pair(Ty, Slot), C));
163 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
165 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
166 ValueList &DL = ValTab[ty];
168 while ((Size = DL.size())) {
169 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
171 Value *D = DL[Size-1];
174 Value *NewDef = getValue(D->getType(), IDNumber, false);
176 Error = true; // Unresolved thinger
177 cerr << "Unresolvable reference found: <"
178 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
180 // Fixup all of the uses of this placeholder def...
181 D->replaceAllUsesWith(NewDef);
183 // Now that all the uses are gone, delete the placeholder...
184 // If we couldn't find a def (error case), then leak a little
185 delete D; // memory, 'cause otherwise we can't remove all uses!
193 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
195 BB = new BasicBlock();
197 while (Buf < EndBuf) {
199 if (ParseInstruction(Buf, EndBuf, Inst,
205 if (Inst == 0) { delete BB; return true; }
206 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
208 BB->getInstList().push_back(Inst);
216 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
218 while (Buf < EndBuf) {
219 // Symtab block header: [num entries][type id number]
220 unsigned NumEntries, Typ;
221 if (read_vbr(Buf, EndBuf, NumEntries) ||
222 read_vbr(Buf, EndBuf, Typ)) return true;
223 const Type *Ty = getType(Typ);
224 if (Ty == 0) return true;
226 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
229 for (unsigned i = 0; i < NumEntries; ++i) {
230 // Symtab entry: [def slot #][name]
232 if (read_vbr(Buf, EndBuf, slot)) return true;
234 if (read(Buf, EndBuf, Name, false)) // Not aligned...
237 Value *D = getValue(Ty, slot, false); // Find mapping...
239 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
242 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
243 if (!isa<Instruction>(D)) cerr << "\n");
245 D->setName(Name, ST);
249 if (Buf > EndBuf) return true;
253 void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
254 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(NewV->getType(),Slot));
255 if (I == GlobalRefs.end()) return; // Never forward referenced?
257 BCR_TRACE(3, "Mutating forward refs!\n");
258 Value *VPH = I->second; // Get the placeholder...
260 // Loop over all of the uses of the Value. What they are depends
261 // on what NewV is. Replacing a use of the old reference takes the
262 // use off the use list, so loop with !use_empty(), not the use_iterator.
263 while (!VPH->use_empty()) {
264 Constant *C = cast<Constant>(VPH->use_back());
265 unsigned numReplaced = C->mutateReferences(VPH, NewV);
266 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
268 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
269 // Remove the placeholder GlobalValue from the module...
270 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
274 delete VPH; // Delete the old placeholder
275 GlobalRefs.erase(I); // Remove the map entry for it
278 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
280 // Clear out the local values table...
282 if (FunctionSignatureList.empty()) {
283 Error = "Function found, but FunctionSignatureList empty!";
284 return true; // Unexpected method!
287 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
288 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
289 if (MTy == 0) return true; // Not ptr to method!
292 if (read_vbr(Buf, EndBuf, isInternal)) return true;
294 unsigned MethSlot = FunctionSignatureList.back().second;
295 FunctionSignatureList.pop_back();
296 Function *M = new Function(MTy, isInternal != 0);
298 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
300 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
301 Function::aiterator AI = M->abegin();
302 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
303 It != Params.end(); ++It, ++AI) {
304 if (insertValue(AI, Values) == -1) {
305 Error = "Error reading method arguments!\n";
306 delete M; return true;
310 while (Buf < EndBuf) {
312 const uchar *OldBuf = Buf;
313 if (readBlock(Buf, EndBuf, Type, Size)) {
314 Error = "Error reading Function level block!";
315 delete M; return true;
319 case BytecodeFormat::ConstantPool:
320 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
321 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
322 delete M; return true;
326 case BytecodeFormat::BasicBlock: {
327 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
329 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
330 insertValue(BB, Values) == -1) {
331 delete M; return true; // Parse error... :(
334 M->getBasicBlockList().push_back(BB);
338 case BytecodeFormat::SymbolTable:
339 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
340 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
341 delete M; return true;
346 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
348 if (OldBuf > Buf) return true; // Wrap around!
351 BCR_TRACE(2, "} end block\n");
353 if (align32(Buf, EndBuf)) {
354 Error = "Error aligning Function level block!";
355 delete M; // Malformed bc file, read past end of block.
360 if (postResolveValues(LateResolveValues) ||
361 postResolveValues(LateResolveModuleValues)) {
362 Error = "Error resolving method values!";
363 delete M; return true; // Unresolvable references!
366 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
367 assert(FunctionPHolder && "Something is broken no placeholder found!");
368 assert(isa<Function>(FunctionPHolder) && "Not a function?");
370 unsigned type; // Type slot
371 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
372 getTypeSlot(PMTy, type);
374 C->getFunctionList().push_back(M);
376 // Replace placeholder with the real method pointer...
377 ModuleValues[type][MethSlot] = M;
379 // Clear out method level types...
380 MethodTypeValues.clear();
382 // If anyone is using the placeholder make them use the real method instead
383 FunctionPHolder->replaceAllUsesWith(M);
385 // We don't need the placeholder anymore!
386 delete FunctionPHolder;
388 ResolveReferencesToValue(M, MethSlot);
393 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
395 if (!FunctionSignatureList.empty()) {
396 Error = "Two ModuleGlobalInfo packets found!";
397 return true; // Two ModuleGlobal blocks?
400 // Read global variables...
402 if (read_vbr(Buf, End, VarType)) return true;
403 while (VarType != Type::VoidTyID) { // List is terminated by Void
404 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
405 // bit2 = isInternal, bit3+ = slot#
406 const Type *Ty = getType(VarType >> 3);
407 if (!Ty || !isa<PointerType>(Ty)) {
408 Error = "Global not pointer type! Ty = " + Ty->getDescription();
412 const PointerType *PTy = cast<const PointerType>(Ty);
413 const Type *ElTy = PTy->getElementType();
415 Constant *Initializer = 0;
416 if (VarType & 2) { // Does it have an initalizer?
417 // Do not improvise... values must have been stored in the constant pool,
418 // which should have been read before now.
421 if (read_vbr(Buf, End, InitSlot)) return true;
423 Value *V = getValue(ElTy, InitSlot, false);
424 if (V == 0) return true;
425 Initializer = cast<Constant>(V);
428 // Create the global variable...
429 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
431 int DestSlot = insertValue(GV, ModuleValues);
432 if (DestSlot == -1) return true;
434 Mod->getGlobalList().push_back(GV);
436 ResolveReferencesToValue(GV, (unsigned)DestSlot);
438 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
439 << " into slot #" << DestSlot << "\n");
441 if (read_vbr(Buf, End, VarType)) return true;
444 // Read the method signatures for all of the methods that are coming, and
445 // create fillers in the Value tables.
446 unsigned FnSignature;
447 if (read_vbr(Buf, End, FnSignature)) return true;
448 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
449 const Type *Ty = getType(FnSignature);
450 if (!Ty || !isa<PointerType>(Ty) ||
451 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
452 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
456 // We create methods by passing the underlying FunctionType to create...
457 Ty = cast<PointerType>(Ty)->getElementType();
459 // When the ModuleGlobalInfo section is read, we load the type of each
460 // method and the 'ModuleValues' slot that it lands in. We then load a
461 // placeholder into its slot to reserve it. When the method is loaded, this
462 // placeholder is replaced.
464 // Insert the placeholder...
465 Value *Val = new FunctionPHolder(Ty, 0);
466 if (insertValue(Val, ModuleValues) == -1) return true;
468 // Figure out which entry of its typeslot it went into...
470 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
472 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
474 // Keep track of this information in a linked list that is emptied as
475 // methods are loaded...
477 FunctionSignatureList.push_back(
478 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
479 if (read_vbr(Buf, End, FnSignature)) return true;
480 BCR_TRACE(2, "Function of type: " << Ty << "\n");
483 if (align32(Buf, End)) return true;
485 // Now that the function signature list is set up, reverse it so that we can
486 // remove elements efficiently from the back of the vector.
487 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
489 // This is for future proofing... in the future extra fields may be added that
490 // we don't understand, so we transparently ignore them.
496 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
500 if (readBlock(Buf, EndBuf, Type, Size)) return true;
501 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
502 Error = "Expected Module packet!";
503 return true; // Hrm, not a class?
506 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
507 FunctionSignatureList.clear(); // Just in case...
509 // Read into instance variables...
510 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
511 if (align32(Buf, EndBuf)) return true;
512 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
514 TheModule = Mod = new Module();
516 while (Buf < EndBuf) {
517 const uchar *OldBuf = Buf;
518 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
520 case BytecodeFormat::ConstantPool:
521 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
522 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
523 delete Mod; return true;
527 case BytecodeFormat::ModuleGlobalInfo:
528 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
530 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
531 delete Mod; return true;
535 case BytecodeFormat::Function: {
536 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
537 if (ParseMethod(Buf, Buf+Size, Mod)) {
538 delete Mod; return true; // Error parsing function
543 case BytecodeFormat::SymbolTable:
544 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
545 if (ParseSymbolTable(Buf, Buf+Size, Mod->getSymbolTableSure())) {
546 delete Mod; return true;
551 Error = "Expected Module Block!";
553 if (OldBuf > Buf) return true; // Wrap around!
556 BCR_TRACE(1, "} end block\n");
557 if (align32(Buf, EndBuf)) { delete Mod; return true; }
560 if (!FunctionSignatureList.empty()) { // Expected more methods!
561 Error = "Function expected, but bytecode stream at end!";
565 BCR_TRACE(0, "} end block\n\n");
569 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
570 LateResolveValues.clear();
572 // Read and check signature...
573 if (read(Buf, EndBuf, Sig) ||
574 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
575 Error = "Invalid bytecode signature!";
576 return 0; // Invalid signature!
580 if (ParseModule(Buf, EndBuf, Result)) return 0;
585 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) {
586 BytecodeParser Parser;
587 return Parser.ParseBytecode(Buffer, Buffer+Length);
590 // Parse and return a class file...
592 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
596 if (Filename != std::string("-")) { // Read from a file...
597 int FD = open(Filename.c_str(), O_RDONLY);
599 if (ErrorStr) *ErrorStr = "Error opening file!";
603 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
605 int Length = StatBuf.st_size;
607 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
610 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
612 if (Buffer == (uchar*)-1) {
613 if (ErrorStr) *ErrorStr = "Error mmapping file!";
617 BytecodeParser Parser;
618 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
620 munmap((char*)Buffer, Length);
622 if (ErrorStr) *ErrorStr = Parser.getError();
623 } else { // Read from stdin
626 uchar Buffer[4096], *FileData = 0;
627 while ((BlockSize = read(0, Buffer, 4096))) {
628 if (BlockSize == -1) { free(FileData); return 0; }
630 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
631 memcpy(FileData+FileSize, Buffer, BlockSize);
632 FileSize += BlockSize;
636 if (ErrorStr) *ErrorStr = "Standard Input empty!";
637 free(FileData); return 0;
642 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
643 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
644 assert((Buf != (uchar*)-1) && "mmap returned error!");
645 memcpy(Buf, FileData, FileSize);
648 uchar *Buf = FileData;
651 BytecodeParser Parser;
652 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
655 munmap((char*)Buf, FileSize); // Free mmap'd data area
657 free(FileData); // Free realloc'd block of memory
660 if (ErrorStr) *ErrorStr = Parser.getError();