* Remove support for internal constant pool
[oota-llvm.git] / lib / Bytecode / Reader / Reader.cpp
1 //===- Reader.cpp - Code to read bytecode files -----------------------------===
2 //
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 //
8 // TODO: Make error message outputs be configurable depending on an option?
9 // TODO: Allow passing in an option to ignore the symbol table
10 //
11 //===------------------------------------------------------------------------===
12
13 #include "llvm/Bytecode/Reader.h"
14 #include "llvm/Bytecode/Format.h"
15 #include "llvm/Module.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/ConstPoolVals.h"
19 #include "llvm/iOther.h"
20 #include "ReaderInternals.h"
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <algorithm>
27
28 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
29   if (Ty->isPrimitiveType()) {
30     Slot = Ty->getPrimitiveID();
31   } else {
32     // Check the method level types first...
33     TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
34                                         MethodTypeValues.end(), Ty);
35     if (I != MethodTypeValues.end()) {
36       Slot = FirstDerivedTyID+ModuleTypeValues.size()+
37              (&*I - &MethodTypeValues[0]);
38     } else {
39       I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
40       if (I == ModuleTypeValues.end()) return true;   // Didn't find type!
41       Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
42     }
43   }
44   //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
45   return false;
46 }
47
48 const Type *BytecodeParser::getType(unsigned ID) {
49   const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
50   if (T) return T;
51   
52   //cerr << "Looking up Type ID: " << ID << endl;
53
54   const Value *D = getValue(Type::TypeTy, ID, false);
55   if (D == 0) return failure<const Type*>(0);
56
57   return D->castTypeAsserting();
58 }
59
60 bool BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
61   unsigned type;
62   if (getTypeSlot(Val->getType(), type)) return failure(true);
63   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
64  
65   if (ValueTab.size() <= type)
66     ValueTab.resize(type+1, ValueList());
67
68   //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
69   //     << "] = " << Val << endl;
70   ValueTab[type].push_back(Val);
71
72   return false;
73 }
74
75 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
76   unsigned Num = oNum;
77   unsigned type;   // The type plane it lives in...
78
79   if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
80
81   if (type == Type::TypeTyID) {  // The 'type' plane has implicit values
82     assert(Create == false);
83     const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
84     if (T) return (Value*)T;   // Asked for a primitive type...
85
86     // Otherwise, derived types need offset...
87     Num -= FirstDerivedTyID;
88
89     // Is it a module level type?
90     if (Num < ModuleTypeValues.size())
91       return (Value*)(const Type*)ModuleTypeValues[Num];
92
93     // Nope, is it a method level type?
94     Num -= ModuleTypeValues.size();
95     if (Num < MethodTypeValues.size())
96       return (Value*)(const Type*)MethodTypeValues[Num];
97
98     return 0;
99   }
100
101   if (type < ModuleValues.size()) {
102     if (Num < ModuleValues[type].size())
103       return ModuleValues[type][Num];
104     Num -= ModuleValues[type].size();
105   }
106
107   if (Values.size() > type && Values[type].size() > Num)
108     return Values[type][Num];
109
110   if (!Create) return failure<Value*>(0);  // Do not create a placeholder?
111
112   Value *d = 0;
113   switch (Ty->getPrimitiveID()) {
114   case Type::LabelTyID: d = new    BBPHolder(Ty, oNum); break;
115   case Type::MethodTyID:
116     cerr << "Creating method pholder! : " << type << ":" << oNum << " " 
117          << Ty->getName() << endl;
118     d = new MethPHolder(Ty, oNum);
119     insertValue(d, LateResolveModuleValues);
120     return d;
121   default:                   d = new   DefPHolder(Ty, oNum); break;
122   }
123
124   assert(d != 0 && "How did we not make something?");
125   if (insertValue(d, LateResolveValues)) return failure<Value*>(0);
126   return d;
127 }
128
129 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
130   bool Error = false;
131   for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
132     ValueList &DL = ValTab[ty];
133     unsigned Size;
134     while ((Size = DL.size())) {
135       unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
136
137       Value *D = DL[Size-1];
138       DL.pop_back();
139
140       Value *NewDef = getValue(D->getType(), IDNumber, false);
141       if (NewDef == 0) {
142         Error = true;  // Unresolved thinger
143         cerr << "Unresolvable reference found: <" << D->getType()->getName()
144              << ">:" << IDNumber << "!\n";
145       } else {
146         // Fixup all of the uses of this placeholder def...
147         D->replaceAllUsesWith(NewDef);
148
149         // Now that all the uses are gone, delete the placeholder...
150         // If we couldn't find a def (error case), then leak a little
151         delete D;  // memory, 'cause otherwise we can't remove all uses!
152       }
153     }
154   }
155
156   return Error;
157 }
158
159 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf, 
160                                      BasicBlock *&BB) {
161   BB = new BasicBlock();
162
163   while (Buf < EndBuf) {
164     Instruction *Inst;
165     if (ParseInstruction(Buf, EndBuf, Inst)) {
166       delete BB;
167       return failure(true);
168     }
169
170     if (Inst == 0) { delete BB; return failure(true); }
171     if (insertValue(Inst, Values)) { delete BB; return failure(true); }
172
173     BB->getInstList().push_back(Inst);
174
175     BCR_TRACE(4, Inst);
176   }
177
178   return false;
179 }
180
181 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
182                                       SymbolTable *ST) {
183   while (Buf < EndBuf) {
184     // Symtab block header: [num entries][type id number]
185     unsigned NumEntries, Typ;
186     if (read_vbr(Buf, EndBuf, NumEntries) ||
187         read_vbr(Buf, EndBuf, Typ)) return failure(true);
188     const Type *Ty = getType(Typ);
189     if (Ty == 0) return failure(true);
190
191     BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
192               " entries\n");
193
194     for (unsigned i = 0; i < NumEntries; ++i) {
195       // Symtab entry: [def slot #][name]
196       unsigned slot;
197       if (read_vbr(Buf, EndBuf, slot)) return failure(true);
198       string Name;
199       if (read(Buf, EndBuf, Name, false))  // Not aligned...
200         return failure(true);
201
202       Value *D = getValue(Ty, slot, false); // Find mapping...
203       if (D == 0) {
204         BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << endl);
205         return failure(true);
206       }
207       BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
208                 if (!D->isInstruction()) cerr << endl);
209
210       D->setName(Name, ST);
211     }
212   }
213
214   if (Buf > EndBuf) return failure(true);
215   return false;
216 }
217
218
219 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf, 
220                                  Module *C) {
221   // Clear out the local values table...
222   Values.clear();
223   if (MethodSignatureList.empty()) return failure(true);  // Unexpected method!
224
225   const MethodType *MTy = MethodSignatureList.front().first;
226   unsigned MethSlot = MethodSignatureList.front().second;
227   MethodSignatureList.pop_front();
228   Method *M = new Method(MTy);
229
230   BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
231
232   const MethodType::ParamTypes &Params = MTy->getParamTypes();
233   for (MethodType::ParamTypes::const_iterator It = Params.begin();
234        It != Params.end(); ++It) {
235     MethodArgument *MA = new MethodArgument(*It);
236     if (insertValue(MA, Values)) { delete M; return failure(true); }
237     M->getArgumentList().push_back(MA);
238   }
239
240   while (Buf < EndBuf) {
241     unsigned Type, Size;
242     const uchar *OldBuf = Buf;
243     if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
244
245     switch (Type) {
246     case BytecodeFormat::ConstantPool:
247       BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
248       if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
249         delete M; return failure(true);
250       }
251       break;
252
253     case BytecodeFormat::BasicBlock: {
254       BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
255       BasicBlock *BB;
256       if (ParseBasicBlock(Buf, Buf+Size, BB) ||
257           insertValue(BB, Values)) {
258         delete M; return failure(true);                // Parse error... :(
259       }
260
261       M->getBasicBlocks().push_back(BB);
262       break;
263     }
264
265     case BytecodeFormat::SymbolTable:
266       BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
267       if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
268         delete M; return failure(true);
269       }
270       break;
271
272     default:
273       BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
274       Buf += Size;
275       if (OldBuf > Buf) return failure(true); // Wrap around!
276       break;
277     }
278     BCR_TRACE(2, "} end block\n");
279
280     if (align32(Buf, EndBuf)) {
281       delete M;    // Malformed bc file, read past end of block.
282       return failure(true);
283     }
284   }
285
286   if (postResolveValues(LateResolveValues) ||
287       postResolveValues(LateResolveModuleValues)) {
288     delete M; return failure(true);     // Unresolvable references!
289   }
290
291   Value *MethPHolder = getValue(MTy, MethSlot, false);
292   assert(MethPHolder && "Something is broken no placeholder found!");
293   assert(MethPHolder->isMethod() && "Not a method?");
294
295   unsigned type;  // Type slot
296   assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
297   getTypeSlot(MTy, type);
298
299   C->getMethodList().push_back(M);
300
301   // Replace placeholder with the real method pointer...
302   ModuleValues[type][MethSlot] = M;
303
304   // If anyone is using the placeholder make them use the real method instead
305   MethPHolder->replaceAllUsesWith(M);
306
307   // We don't need the placeholder anymore!
308   delete MethPHolder;
309
310   return false;
311 }
312
313 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
314                                           Module *C) {
315
316   if (!MethodSignatureList.empty()) 
317     return failure(true);  // Two ModuleGlobal blocks?
318
319   // Read the method signatures for all of the methods that are coming, and 
320   // create fillers in the Value tables.
321   unsigned MethSignature;
322   if (read_vbr(Buf, End, MethSignature)) return failure(true);
323   while (MethSignature != Type::VoidTyID) { // List is terminated by Void
324     const Type *Ty = getType(MethSignature);
325     if (!Ty || !Ty->isMethodType()) { 
326       cerr << "Method not meth type!  Ty = " << Ty << endl;
327       if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl; 
328       return failure(true); 
329     }
330
331     // When the ModuleGlobalInfo section is read, we load the type of each 
332     // method and the 'ModuleValues' slot that it lands in.  We then load a 
333     // placeholder into its slot to reserve it.  When the method is loaded, this
334     // placeholder is replaced.
335
336     // Insert the placeholder...
337     Value *Def = new MethPHolder(Ty, 0);
338     insertValue(Def, ModuleValues);
339
340     // Figure out which entry of its typeslot it went into...
341     unsigned TypeSlot;
342     if (getTypeSlot(Def->getType(), TypeSlot)) return failure(true);
343
344     unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
345     
346     // Keep track of this information in a linked list that is emptied as 
347     // methods are loaded...
348     //
349     MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
350     if (read_vbr(Buf, End, MethSignature)) return failure(true);
351     BCR_TRACE(2, "Method of type: " << Ty << endl);
352   }
353
354   if (align32(Buf, End)) return failure(true);
355
356   // This is for future proofing... in the future extra fields may be added that
357   // we don't understand, so we transparently ignore them.
358   //
359   Buf = End;
360   return false;
361 }
362
363 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf, 
364                                 Module *&C) {
365
366   unsigned Type, Size;
367   if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
368   if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
369     return failure(true);                      // Hrm, not a class?
370
371   BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
372   MethodSignatureList.clear();                 // Just in case...
373
374   // Read into instance variables...
375   if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
376   if (align32(Buf, EndBuf)) return failure(true);
377   BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
378
379   C = new Module();
380   while (Buf < EndBuf) {
381     const uchar *OldBuf = Buf;
382     if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
383     switch (Type) {
384     case BytecodeFormat::ConstantPool:
385       BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
386       if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
387         delete C; return failure(true);
388       }
389       break;
390
391     case BytecodeFormat::ModuleGlobalInfo:
392       BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
393
394       if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
395         delete C; return failure(true);
396       }
397       break;
398
399     case BytecodeFormat::Method: {
400       BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
401       if (ParseMethod(Buf, Buf+Size, C)) {
402         delete C; return failure(true);               // Error parsing method
403       }
404       break;
405     }
406
407     case BytecodeFormat::SymbolTable:
408       BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
409       if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
410         delete C; return failure(true);
411       }
412       break;
413
414     default:
415       cerr << "  Unknown class block: " << Type << endl;
416       Buf += Size;
417       if (OldBuf > Buf) return failure(true); // Wrap around!
418       break;
419     }
420     BCR_TRACE(1, "} end block\n");
421     if (align32(Buf, EndBuf)) { delete C; return failure(true); }
422   }
423
424   if (!MethodSignatureList.empty())      // Expected more methods!
425     return failure(true);
426
427   BCR_TRACE(0, "} end block\n\n");
428   return false;
429 }
430
431 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
432   LateResolveValues.clear();
433   unsigned Sig;
434   // Read and check signature...
435   if (read(Buf, EndBuf, Sig) ||
436       Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
437     return failure<Module*>(0);                          // Invalid signature!
438
439   Module *Result;
440   if (ParseModule(Buf, EndBuf, Result)) return 0;
441   return Result;
442 }
443
444
445 Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
446   BytecodeParser Parser;
447   return Parser.ParseBytecode(Buffer, Buffer+Length);
448 }
449
450 // Parse and return a class file...
451 //
452 Module *ParseBytecodeFile(const string &Filename) {
453   struct stat StatBuf;
454   Module *Result = 0;
455
456   if (Filename != string("-")) {        // Read from a file...
457     int FD = open(Filename.c_str(), O_RDONLY);
458     if (FD == -1) return failure<Module*>(0);
459
460     if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
461
462     int Length = StatBuf.st_size;
463     if (Length == 0) { close(FD); return failure<Module*>(0); }
464     uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ, 
465                                 MAP_PRIVATE, FD, 0);
466     if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
467
468     BytecodeParser Parser;
469     Result  = Parser.ParseBytecode(Buffer, Buffer+Length);
470
471     munmap((char*)Buffer, Length);
472     close(FD);
473   } else {                              // Read from stdin
474     size_t FileSize = 0;
475     int BlockSize;
476     uchar Buffer[4096], *FileData = 0;
477     while ((BlockSize = read(0, Buffer, 4))) {
478       if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
479
480       FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
481       memcpy(FileData+FileSize, Buffer, BlockSize);
482       FileSize += BlockSize;
483     }
484
485     if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
486
487 #define ALIGN_PTRS 1
488 #if ALIGN_PTRS
489     uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE, 
490                               MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
491     assert((Buf != (uchar*)-1) && "mmap returned error!");
492     free(FileData);
493     memcpy(Buf, FileData, FileSize);
494 #else
495     uchar *Buf = FileData;
496 #endif
497
498     BytecodeParser Parser;
499     Result = Parser.ParseBytecode(Buf, Buf+FileSize);
500
501 #if ALIGN_PTRS
502     munmap((char*)Buf, FileSize);   // Free mmap'd data area
503 #else
504     free(FileData);          // Free realloc'd block of memory
505 #endif
506   }
507
508   return Result;
509 }