Wow, for some reason, when reading bytecode from stdin, we were only reading
[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: Return error messages to caller instead of printing them out directly.
9 // TODO: Allow passing in an option to ignore the symbol table
10 //
11 //===----------------------------------------------------------------------===//
12
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>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <algorithm>
26 using std::cerr;
27 using std::pair;
28 using std::make_pair;
29
30 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
31   if (Ty->isPrimitiveType()) {
32     Slot = Ty->getPrimitiveID();
33   } else {
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]);
40     } else {
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]);
44     }
45   }
46   //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
47   return false;
48 }
49
50 const Type *BytecodeParser::getType(unsigned ID) {
51   const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
52   if (T) return T;
53   
54   //cerr << "Looking up Type ID: " << ID << "\n";
55
56   const Value *D = getValue(Type::TypeTy, ID, false);
57   if (D == 0) return 0;
58
59   return cast<Type>(D);
60 }
61
62 int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
63   unsigned type;
64   if (getTypeSlot(Val->getType(), type)) return -1;
65   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
66  
67   if (ValueTab.size() <= type)
68     ValueTab.resize(type+1, ValueList());
69
70   //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
71   //     << "] = " << Val << "\n";
72   ValueTab[type].push_back(Val);
73
74   return ValueTab[type].size()-1;
75 }
76
77 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
78   unsigned Num = oNum;
79   unsigned type;   // The type plane it lives in...
80
81   if (getTypeSlot(Ty, type)) return 0;
82
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...
87
88     // Otherwise, derived types need offset...
89     Num -= FirstDerivedTyID;
90
91     // Is it a module level type?
92     if (Num < ModuleTypeValues.size())
93       return (Value*)ModuleTypeValues[Num].get();
94
95     // Nope, is it a method level type?
96     Num -= ModuleTypeValues.size();
97     if (Num < MethodTypeValues.size())
98       return (Value*)MethodTypeValues[Num].get();
99
100     return 0;
101   }
102
103   if (type < ModuleValues.size()) {
104     if (Num < ModuleValues[type].size())
105       return ModuleValues[type][Num];
106     Num -= ModuleValues[type].size();
107   }
108
109   if (Values.size() > type && Values[type].size() > Num)
110     return Values[type][Num];
111
112   if (!Create) return 0;  // Do not create a placeholder?
113
114   Value *d = 0;
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;
121     return d;
122   case Type::LabelTyID:
123     d = new BBPHolder(Ty, oNum);
124     break;
125   default:
126     d = new ValPHolder(Ty, oNum);
127     break;
128   }
129
130   assert(d != 0 && "How did we not make something?");
131   if (insertValue(d, LateResolveValues) == -1) return 0;
132   return d;
133 }
134
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.
140 ///
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...
144
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);
149   } else {
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);
154     
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));
157     return C;
158   }
159 }
160
161
162
163 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
164   bool Error = false;
165   for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
166     ValueList &DL = ValTab[ty];
167     unsigned Size;
168     while ((Size = DL.size())) {
169       unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
170
171       Value *D = DL[Size-1];
172       DL.pop_back();
173
174       Value *NewDef = getValue(D->getType(), IDNumber, false);
175       if (NewDef == 0) {
176         Error = true;  // Unresolved thinger
177         cerr << "Unresolvable reference found: <"
178               << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
179       } else {
180         // Fixup all of the uses of this placeholder def...
181         D->replaceAllUsesWith(NewDef);
182
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!
186       }
187     }
188   }
189
190   return Error;
191 }
192
193 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf, 
194                                      BasicBlock *&BB) {
195   BB = new BasicBlock();
196
197   while (Buf < EndBuf) {
198     Instruction *Inst;
199     if (ParseInstruction(Buf, EndBuf, Inst,
200                          /*HACK*/BB)) {
201       delete BB;
202       return true;
203     }
204
205     if (Inst == 0) { delete BB; return true; }
206     if (insertValue(Inst, Values) == -1) { delete BB; return true; }
207
208     BB->getInstList().push_back(Inst);
209
210     BCR_TRACE(4, Inst);
211   }
212
213   return false;
214 }
215
216 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
217                                       SymbolTable *ST) {
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;
225
226     BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
227               " entries\n");
228
229     for (unsigned i = 0; i < NumEntries; ++i) {
230       // Symtab entry: [def slot #][name]
231       unsigned slot;
232       if (read_vbr(Buf, EndBuf, slot)) return true;
233       std::string Name;
234       if (read(Buf, EndBuf, Name, false))  // Not aligned...
235         return true;
236
237       Value *D = getValue(Ty, slot, false); // Find mapping...
238       if (D == 0) {
239         BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
240         return true;
241       }
242       BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
243                 if (!isa<Instruction>(D)) cerr << "\n");
244
245       D->setName(Name, ST);
246     }
247   }
248
249   if (Buf > EndBuf) return true;
250   return false;
251 }
252
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?
256
257   BCR_TRACE(3, "Mutating forward refs!\n");
258   Value *VPH = I->second;   // Get the placeholder...
259
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?");
267       
268     if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
269       // Remove the placeholder GlobalValue from the module...
270       GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
271     }
272   }
273
274   delete VPH;                         // Delete the old placeholder
275   GlobalRefs.erase(I);                // Remove the map entry for it
276 }
277
278 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf, 
279                                  Module *C) {
280   // Clear out the local values table...
281   Values.clear();
282   if (FunctionSignatureList.empty()) {
283     Error = "Function found, but FunctionSignatureList empty!";
284     return true;  // Unexpected method!
285   }
286
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!
290
291   unsigned isInternal;
292   if (read_vbr(Buf, EndBuf, isInternal)) return true;
293
294   unsigned MethSlot = FunctionSignatureList.back().second;
295   FunctionSignatureList.pop_back();
296   Function *M = new Function(MTy, isInternal != 0);
297
298   BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
299
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; 
307     }
308   }
309
310   while (Buf < EndBuf) {
311     unsigned Type, Size;
312     const uchar *OldBuf = Buf;
313     if (readBlock(Buf, EndBuf, Type, Size)) {
314       Error = "Error reading Function level block!";
315       delete M; return true; 
316     }
317
318     switch (Type) {
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;
323       }
324       break;
325
326     case BytecodeFormat::BasicBlock: {
327       BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
328       BasicBlock *BB;
329       if (ParseBasicBlock(Buf, Buf+Size, BB) ||
330           insertValue(BB, Values) == -1) {
331         delete M; return true;                // Parse error... :(
332       }
333
334       M->getBasicBlockList().push_back(BB);
335       break;
336     }
337
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;
342       }
343       break;
344
345     default:
346       BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
347       Buf += Size;
348       if (OldBuf > Buf) return true; // Wrap around!
349       break;
350     }
351     BCR_TRACE(2, "} end block\n");
352
353     if (align32(Buf, EndBuf)) {
354       Error = "Error aligning Function level block!";
355       delete M;    // Malformed bc file, read past end of block.
356       return true;
357     }
358   }
359
360   if (postResolveValues(LateResolveValues) ||
361       postResolveValues(LateResolveModuleValues)) {
362     Error = "Error resolving method values!";
363     delete M; return true;     // Unresolvable references!
364   }
365
366   Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
367   assert(FunctionPHolder && "Something is broken no placeholder found!");
368   assert(isa<Function>(FunctionPHolder) && "Not a function?");
369
370   unsigned type;  // Type slot
371   assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
372   getTypeSlot(PMTy, type);
373
374   C->getFunctionList().push_back(M);
375
376   // Replace placeholder with the real method pointer...
377   ModuleValues[type][MethSlot] = M;
378
379   // Clear out method level types...
380   MethodTypeValues.clear();
381
382   // If anyone is using the placeholder make them use the real method instead
383   FunctionPHolder->replaceAllUsesWith(M);
384
385   // We don't need the placeholder anymore!
386   delete FunctionPHolder;
387
388   ResolveReferencesToValue(M, MethSlot);
389
390   return false;
391 }
392
393 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
394                                            Module *Mod) {
395   if (!FunctionSignatureList.empty()) {
396     Error = "Two ModuleGlobalInfo packets found!";
397     return true;  // Two ModuleGlobal blocks?
398   }
399
400   // Read global variables...
401   unsigned VarType;
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();
409       return true; 
410     }
411
412     const PointerType *PTy = cast<const PointerType>(Ty);
413     const Type *ElTy = PTy->getElementType();
414
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.
419       //
420       unsigned InitSlot;
421       if (read_vbr(Buf, End, InitSlot)) return true;
422       
423       Value *V = getValue(ElTy, InitSlot, false);
424       if (V == 0) return true;
425       Initializer = cast<Constant>(V);
426     }
427
428     // Create the global variable...
429     GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
430                                             Initializer);
431     int DestSlot = insertValue(GV, ModuleValues);
432     if (DestSlot == -1) return true;
433
434     Mod->getGlobalList().push_back(GV);
435
436     ResolveReferencesToValue(GV, (unsigned)DestSlot);
437
438     BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription() 
439               << " into slot #" << DestSlot << "\n");
440
441     if (read_vbr(Buf, End, VarType)) return true;
442   }
443
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();
453       return true; 
454     }
455     
456     // We create methods by passing the underlying FunctionType to create...
457     Ty = cast<PointerType>(Ty)->getElementType();
458
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.
463
464     // Insert the placeholder...
465     Value *Val = new FunctionPHolder(Ty, 0);
466     if (insertValue(Val, ModuleValues) == -1) return true;
467
468     // Figure out which entry of its typeslot it went into...
469     unsigned TypeSlot;
470     if (getTypeSlot(Val->getType(), TypeSlot)) return true;
471
472     unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
473     
474     // Keep track of this information in a linked list that is emptied as 
475     // methods are loaded...
476     //
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");
481   }
482
483   if (align32(Buf, End)) return true;
484
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());
488
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.
491   //
492   Buf = End;
493   return false;
494 }
495
496 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf, 
497                                 Module *&Mod) {
498
499   unsigned Type, Size;
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?
504   }
505
506   BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
507   FunctionSignatureList.clear();                 // Just in case...
508
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");
513
514   TheModule = Mod = new Module();
515
516   while (Buf < EndBuf) {
517     const uchar *OldBuf = Buf;
518     if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
519     switch (Type) {
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;
524       }
525       break;
526
527     case BytecodeFormat::ModuleGlobalInfo:
528       BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
529
530       if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
531         delete Mod; return true;
532       }
533       break;
534
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
539       }
540       break;
541     }
542
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;
547       }
548       break;
549
550     default:
551       Error = "Expected Module Block!";
552       Buf += Size;
553       if (OldBuf > Buf) return true; // Wrap around!
554       break;
555     }
556     BCR_TRACE(1, "} end block\n");
557     if (align32(Buf, EndBuf)) { delete Mod; return true; }
558   }
559
560   if (!FunctionSignatureList.empty()) {     // Expected more methods!
561     Error = "Function expected, but bytecode stream at end!";
562     return true;
563   }
564
565   BCR_TRACE(0, "} end block\n\n");
566   return false;
567 }
568
569 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
570   LateResolveValues.clear();
571   unsigned Sig;
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!
577   }
578
579   Module *Result;
580   if (ParseModule(Buf, EndBuf, Result)) return 0;
581   return Result;
582 }
583
584
585 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) {
586   BytecodeParser Parser;
587   return Parser.ParseBytecode(Buffer, Buffer+Length);
588 }
589
590 // Parse and return a class file...
591 //
592 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
593   struct stat StatBuf;
594   Module *Result = 0;
595
596   if (Filename != std::string("-")) {        // Read from a file...
597     int FD = open(Filename.c_str(), O_RDONLY);
598     if (FD == -1) {
599       if (ErrorStr) *ErrorStr = "Error opening file!";
600       return 0;
601     }
602
603     if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
604
605     int Length = StatBuf.st_size;
606     if (Length == 0) { 
607       if (ErrorStr) *ErrorStr = "Error stat'ing file!";
608       close(FD); return 0; 
609     }
610     uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ, 
611                                 MAP_PRIVATE, FD, 0);
612     if (Buffer == (uchar*)-1) {
613       if (ErrorStr) *ErrorStr = "Error mmapping file!";
614       close(FD); return 0;
615     }
616
617     BytecodeParser Parser;
618     Result  = Parser.ParseBytecode(Buffer, Buffer+Length);
619
620     munmap((char*)Buffer, Length);
621     close(FD);
622     if (ErrorStr) *ErrorStr = Parser.getError();
623   } else {                              // Read from stdin
624     size_t FileSize = 0;
625     int BlockSize;
626     uchar Buffer[4096], *FileData = 0;
627     while ((BlockSize = read(0, Buffer, 4096))) {
628       if (BlockSize == -1) { free(FileData); return 0; }
629
630       FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
631       memcpy(FileData+FileSize, Buffer, BlockSize);
632       FileSize += BlockSize;
633     }
634
635     if (FileSize == 0) {
636       if (ErrorStr) *ErrorStr = "Standard Input empty!";
637       free(FileData); return 0;
638     }
639
640 #define ALIGN_PTRS 0
641 #if ALIGN_PTRS
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);
646     free(FileData);
647 #else
648     uchar *Buf = FileData;
649 #endif
650
651     BytecodeParser Parser;
652     Result = Parser.ParseBytecode(Buf, Buf+FileSize);
653
654 #if ALIGN_PTRS
655     munmap((char*)Buf, FileSize);   // Free mmap'd data area
656 #else
657     free(FileData);          // Free realloc'd block of memory
658 #endif
659
660     if (ErrorStr) *ErrorStr = Parser.getError();
661   }
662
663   return Result;
664 }