Split the PHINode class out from the iOther.h file into the iPHINode.h file
[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 "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/ConstPoolVals.h"
20 #include "llvm/iPHINode.h"
21 #include "llvm/iOther.h"
22 #include <sys/types.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <algorithm>
28
29 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
30   if (Ty->isPrimitiveType()) {
31     Slot = Ty->getPrimitiveID();
32   } else {
33     // Check the method level types first...
34     TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
35                                         MethodTypeValues.end(), Ty);
36     if (I != MethodTypeValues.end()) {
37       Slot = FirstDerivedTyID+ModuleTypeValues.size()+
38              (&*I - &MethodTypeValues[0]);
39     } else {
40       I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
41       if (I == ModuleTypeValues.end()) return true;   // Didn't find type!
42       Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
43     }
44   }
45   //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
46   return false;
47 }
48
49 const Type *BytecodeParser::getType(unsigned ID) {
50   const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
51   if (T) return T;
52   
53   //cerr << "Looking up Type ID: " << ID << endl;
54
55   const Value *D = getValue(Type::TypeTy, ID, false);
56   if (D == 0) return failure<const Type*>(0);
57
58   return cast<Type>(D);
59 }
60
61 int BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
62   unsigned type;
63   if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
64   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
65  
66   if (ValueTab.size() <= type)
67     ValueTab.resize(type+1, ValueList());
68
69   //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
70   //     << "] = " << Val << endl;
71   ValueTab[type].push_back(Val);
72
73   return ValueTab[type].size()-1;
74 }
75
76 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
77   unsigned Num = oNum;
78   unsigned type;   // The type plane it lives in...
79
80   if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
81
82   if (type == Type::TypeTyID) {  // The 'type' plane has implicit values
83     assert(Create == false);
84     const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
85     if (T) return (Value*)T;   // Asked for a primitive type...
86
87     // Otherwise, derived types need offset...
88     Num -= FirstDerivedTyID;
89
90     // Is it a module level type?
91     if (Num < ModuleTypeValues.size())
92       return (Value*)ModuleTypeValues[Num].get();
93
94     // Nope, is it a method level type?
95     Num -= ModuleTypeValues.size();
96     if (Num < MethodTypeValues.size())
97       return (Value*)MethodTypeValues[Num].get();
98
99     return 0;
100   }
101
102   if (type < ModuleValues.size()) {
103     if (Num < ModuleValues[type].size())
104       return ModuleValues[type][Num];
105     Num -= ModuleValues[type].size();
106   }
107
108   if (Values.size() > type && Values[type].size() > Num)
109     return Values[type][Num];
110
111   if (!Create) return failure<Value*>(0);  // Do not create a placeholder?
112
113   Value *d = 0;
114   switch (Ty->getPrimitiveID()) {
115   case Type::LabelTyID: d = new    BBPHolder(Ty, oNum); break;
116   case Type::MethodTyID:
117     cerr << "Creating method pholder! : " << type << ":" << oNum << " " 
118          << Ty->getName() << endl;
119     d = new MethPHolder(Ty, oNum);
120     if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
121     return d;
122   default:                   d = new   DefPHolder(Ty, oNum); break;
123   }
124
125   assert(d != 0 && "How did we not make something?");
126   if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
127   return d;
128 }
129
130 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
131   bool Error = false;
132   for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
133     ValueList &DL = ValTab[ty];
134     unsigned Size;
135     while ((Size = DL.size())) {
136       unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
137
138       Value *D = DL[Size-1];
139       DL.pop_back();
140
141       Value *NewDef = getValue(D->getType(), IDNumber, false);
142       if (NewDef == 0) {
143         Error = true;  // Unresolved thinger
144         cerr << "Unresolvable reference found: <"
145               << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
146       } else {
147         // Fixup all of the uses of this placeholder def...
148         D->replaceAllUsesWith(NewDef);
149
150         // Now that all the uses are gone, delete the placeholder...
151         // If we couldn't find a def (error case), then leak a little
152         delete D;  // memory, 'cause otherwise we can't remove all uses!
153       }
154     }
155   }
156
157   return Error;
158 }
159
160 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf, 
161                                      BasicBlock *&BB) {
162   BB = new BasicBlock();
163
164   while (Buf < EndBuf) {
165     Instruction *Inst;
166     if (ParseInstruction(Buf, EndBuf, Inst)) {
167       delete BB;
168       return failure(true);
169     }
170
171     if (Inst == 0) { delete BB; return failure(true); }
172     if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
173
174     BB->getInstList().push_back(Inst);
175
176     BCR_TRACE(4, Inst);
177   }
178
179   return false;
180 }
181
182 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
183                                       SymbolTable *ST) {
184   while (Buf < EndBuf) {
185     // Symtab block header: [num entries][type id number]
186     unsigned NumEntries, Typ;
187     if (read_vbr(Buf, EndBuf, NumEntries) ||
188         read_vbr(Buf, EndBuf, Typ)) return failure(true);
189     const Type *Ty = getType(Typ);
190     if (Ty == 0) return failure(true);
191
192     BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
193               " entries\n");
194
195     for (unsigned i = 0; i < NumEntries; ++i) {
196       // Symtab entry: [def slot #][name]
197       unsigned slot;
198       if (read_vbr(Buf, EndBuf, slot)) return failure(true);
199       string Name;
200       if (read(Buf, EndBuf, Name, false))  // Not aligned...
201         return failure(true);
202
203       Value *D = getValue(Ty, slot, false); // Find mapping...
204       if (D == 0) {
205         BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << endl);
206         return failure(true);
207       }
208       BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
209                 if (!isa<Instruction>(D)) cerr << endl);
210
211       D->setName(Name, ST);
212     }
213   }
214
215   if (Buf > EndBuf) return failure(true);
216   return false;
217 }
218
219 // DeclareNewGlobalValue - Patch up forward references to global values in the
220 // form of ConstPoolPointerRef.
221 //
222 void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
223   // Check to see if there is a forward reference to this global variable...
224   // if there is, eliminate it and patch the reference to use the new def'n.
225   GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
226
227   if (I != GlobalRefs.end()) {
228     GlobalVariable *OldGV = I->second;   // Get the placeholder...
229     BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
230       
231     // Loop over all of the uses of the GlobalValue.  The only thing they are
232     // allowed to be at this point is ConstPoolPointerRef's.
233     assert(OldGV->use_size() == 1 && "Only one reference should exist!");
234     while (!OldGV->use_empty()) {
235       User *U = OldGV->use_back();  // Must be a ConstPoolPointerRef...
236       ConstPoolPointerRef *CPPR = cast<ConstPoolPointerRef>(U);
237       assert(CPPR->getValue() == OldGV && "Something isn't happy");
238       
239       BCR_TRACE(4, "Mutating Forward Ref!\n");
240       
241       // Change the const pool reference to point to the real global variable
242       // now.  This should drop a use from the OldGV.
243       CPPR->mutateReference(GV);
244     }
245     
246     // Remove GV from the module...
247     GV->getParent()->getGlobalList().remove(OldGV);
248     delete OldGV;                        // Delete the old placeholder
249     
250     // Remove the map entry for the global now that it has been created...
251     GlobalRefs.erase(I);
252   }
253 }
254
255 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf, 
256                                  Module *C) {
257   // Clear out the local values table...
258   Values.clear();
259   if (MethodSignatureList.empty()) {
260     Error = "Method found, but MethodSignatureList empty!";
261     return failure(true);  // Unexpected method!
262   }
263
264   const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
265   const MethodType  *MTy  = dyn_cast<const MethodType>(PMTy->getValueType());
266   if (MTy == 0) return failure(true);  // Not ptr to method!
267
268   unsigned isInternal;
269   if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
270
271   unsigned MethSlot = MethodSignatureList.front().second;
272   MethodSignatureList.pop_front();
273   Method *M = new Method(MTy, isInternal != 0);
274
275   BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
276
277   const MethodType::ParamTypes &Params = MTy->getParamTypes();
278   for (MethodType::ParamTypes::const_iterator It = Params.begin();
279        It != Params.end(); ++It) {
280     MethodArgument *MA = new MethodArgument(*It);
281     if (insertValue(MA, Values) == -1) {
282       Error = "Error reading method arguments!\n";
283       delete M; return failure(true); 
284     }
285     M->getArgumentList().push_back(MA);
286   }
287
288   while (Buf < EndBuf) {
289     unsigned Type, Size;
290     const uchar *OldBuf = Buf;
291     if (readBlock(Buf, EndBuf, Type, Size)) {
292       Error = "Error reading Method level block!";
293       delete M; return failure(true); 
294     }
295
296     switch (Type) {
297     case BytecodeFormat::ConstantPool:
298       BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
299       if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
300         delete M; return failure(true);
301       }
302       break;
303
304     case BytecodeFormat::BasicBlock: {
305       BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
306       BasicBlock *BB;
307       if (ParseBasicBlock(Buf, Buf+Size, BB) ||
308           insertValue(BB, Values) == -1) {
309         delete M; return failure(true);                // Parse error... :(
310       }
311
312       M->getBasicBlocks().push_back(BB);
313       break;
314     }
315
316     case BytecodeFormat::SymbolTable:
317       BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
318       if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
319         delete M; return failure(true);
320       }
321       break;
322
323     default:
324       BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
325       Buf += Size;
326       if (OldBuf > Buf) return failure(true); // Wrap around!
327       break;
328     }
329     BCR_TRACE(2, "} end block\n");
330
331     if (align32(Buf, EndBuf)) {
332       Error = "Error aligning Method level block!";
333       delete M;    // Malformed bc file, read past end of block.
334       return failure(true);
335     }
336   }
337
338   if (postResolveValues(LateResolveValues) ||
339       postResolveValues(LateResolveModuleValues)) {
340     Error = "Error resolving method values!";
341     delete M; return failure(true);     // Unresolvable references!
342   }
343
344   Value *MethPHolder = getValue(PMTy, MethSlot, false);
345   assert(MethPHolder && "Something is broken no placeholder found!");
346   assert(isa<Method>(MethPHolder) && "Not a method?");
347
348   unsigned type;  // Type slot
349   assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
350   getTypeSlot(PMTy, type);
351
352   C->getMethodList().push_back(M);
353
354   // Replace placeholder with the real method pointer...
355   ModuleValues[type][MethSlot] = M;
356
357   // Clear out method level types...
358   MethodTypeValues.clear();
359
360   // If anyone is using the placeholder make them use the real method instead
361   MethPHolder->replaceAllUsesWith(M);
362
363   // We don't need the placeholder anymore!
364   delete MethPHolder;
365
366   // If the method is empty, we don't need the method argument entries...
367   if (M->isExternal())
368     M->getArgumentList().delete_all();
369
370   DeclareNewGlobalValue(M, MethSlot);
371
372   return false;
373 }
374
375 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
376                                            Module *Mod) {
377   if (!MethodSignatureList.empty()) {
378     Error = "Two ModuleGlobalInfo packets found!";
379     return failure(true);  // Two ModuleGlobal blocks?
380   }
381
382   // Read global variables...
383   unsigned VarType;
384   if (read_vbr(Buf, End, VarType)) return failure(true);
385   while (VarType != Type::VoidTyID) { // List is terminated by Void
386     // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
387     // bit2 = isInternal, bit3+ = slot#
388     const Type *Ty = getType(VarType >> 3);
389     if (!Ty || !Ty->isPointerType()) { 
390       Error = "Global not pointer type!  Ty = " + Ty->getDescription();
391       return failure(true); 
392     }
393
394     const PointerType *PTy = cast<const PointerType>(Ty);
395     const Type *ElTy = PTy->getValueType();
396
397     ConstPoolVal *Initializer = 0;
398     if (VarType & 2) { // Does it have an initalizer?
399       // Do not improvise... values must have been stored in the constant pool,
400       // which should have been read before now.
401       //
402       unsigned InitSlot;
403       if (read_vbr(Buf, End, InitSlot)) return failure(true);
404       
405       Value *V = getValue(ElTy, InitSlot, false);
406       if (V == 0) return failure(true);
407       Initializer = cast<ConstPoolVal>(V);
408     }
409
410     // Create the global variable...
411     GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
412                                             Initializer);
413     int DestSlot = insertValue(GV, ModuleValues);
414     if (DestSlot == -1) return failure(true);
415
416     Mod->getGlobalList().push_back(GV);
417
418     DeclareNewGlobalValue(GV, unsigned(DestSlot));
419
420     BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription() 
421               << " into slot #" << DestSlot << endl);
422
423     if (read_vbr(Buf, End, VarType)) return failure(true);
424   }
425
426   // Read the method signatures for all of the methods that are coming, and 
427   // create fillers in the Value tables.
428   unsigned MethSignature;
429   if (read_vbr(Buf, End, MethSignature)) return failure(true);
430   while (MethSignature != Type::VoidTyID) { // List is terminated by Void
431     const Type *Ty = getType(MethSignature);
432     if (!Ty || !isa<PointerType>(Ty) ||
433         !isa<MethodType>(cast<PointerType>(Ty)->getValueType())) { 
434       Error = "Method not ptr to meth type!  Ty = " + Ty->getDescription();
435       return failure(true); 
436     }
437     
438     // We create methods by passing the underlying MethodType to create...
439     Ty = cast<PointerType>(Ty)->getValueType();
440
441     // When the ModuleGlobalInfo section is read, we load the type of each 
442     // method and the 'ModuleValues' slot that it lands in.  We then load a 
443     // placeholder into its slot to reserve it.  When the method is loaded, this
444     // placeholder is replaced.
445
446     // Insert the placeholder...
447     Value *Val = new MethPHolder(Ty, 0);
448     if (insertValue(Val, ModuleValues) == -1) return failure(true);
449
450     // Figure out which entry of its typeslot it went into...
451     unsigned TypeSlot;
452     if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
453
454     unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
455     
456     // Keep track of this information in a linked list that is emptied as 
457     // methods are loaded...
458     //
459     MethodSignatureList.push_back(
460            make_pair(cast<const PointerType>(Val->getType()), SlotNo));
461     if (read_vbr(Buf, End, MethSignature)) return failure(true);
462     BCR_TRACE(2, "Method of type: " << Ty << endl);
463   }
464
465   if (align32(Buf, End)) return failure(true);
466
467   // This is for future proofing... in the future extra fields may be added that
468   // we don't understand, so we transparently ignore them.
469   //
470   Buf = End;
471   return false;
472 }
473
474 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf, 
475                                 Module *&C) {
476
477   unsigned Type, Size;
478   if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
479   if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
480     Error = "Expected Module packet!";
481     return failure(true);                      // Hrm, not a class?
482   }
483
484   BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
485   MethodSignatureList.clear();                 // Just in case...
486
487   // Read into instance variables...
488   if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
489   if (align32(Buf, EndBuf)) return failure(true);
490   BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
491
492   TheModule = C = new Module();
493   while (Buf < EndBuf) {
494     const uchar *OldBuf = Buf;
495     if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
496     switch (Type) {
497     case BytecodeFormat::ConstantPool:
498       BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
499       if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
500         delete C; return failure(true);
501       }
502       break;
503
504     case BytecodeFormat::ModuleGlobalInfo:
505       BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
506
507       if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
508         delete C; return failure(true);
509       }
510       break;
511
512     case BytecodeFormat::Method: {
513       BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
514       if (ParseMethod(Buf, Buf+Size, C)) {
515         delete C; return failure(true);               // Error parsing method
516       }
517       break;
518     }
519
520     case BytecodeFormat::SymbolTable:
521       BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
522       if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
523         delete C; return failure(true);
524       }
525       break;
526
527     default:
528       Error = "Expected Module Block!";
529       Buf += Size;
530       if (OldBuf > Buf) return failure(true); // Wrap around!
531       break;
532     }
533     BCR_TRACE(1, "} end block\n");
534     if (align32(Buf, EndBuf)) { delete C; return failure(true); }
535   }
536
537   if (!MethodSignatureList.empty()) {     // Expected more methods!
538     Error = "Method expected, but bytecode stream at end!";
539     return failure(true);
540   }
541
542   BCR_TRACE(0, "} end block\n\n");
543   return false;
544 }
545
546 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
547   LateResolveValues.clear();
548   unsigned Sig;
549   // Read and check signature...
550   if (read(Buf, EndBuf, Sig) ||
551       Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
552     Error = "Invalid bytecode signature!";
553     return failure<Module*>(0);                          // Invalid signature!
554   }
555
556   Module *Result;
557   if (ParseModule(Buf, EndBuf, Result)) return 0;
558   return Result;
559 }
560
561
562 Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
563   BytecodeParser Parser;
564   return Parser.ParseBytecode(Buffer, Buffer+Length);
565 }
566
567 // Parse and return a class file...
568 //
569 Module *ParseBytecodeFile(const string &Filename, string *ErrorStr) {
570   struct stat StatBuf;
571   Module *Result = 0;
572
573   if (Filename != string("-")) {        // Read from a file...
574     int FD = open(Filename.c_str(), O_RDONLY);
575     if (FD == -1) {
576       if (ErrorStr) *ErrorStr = "Error opening file!";
577       return failure<Module*>(0);
578     }
579
580     if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
581
582     int Length = StatBuf.st_size;
583     if (Length == 0) { 
584       if (ErrorStr) *ErrorStr = "Error stat'ing file!";
585       close(FD); return failure<Module*>(0); 
586     }
587     uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ, 
588                                 MAP_PRIVATE, FD, 0);
589     if (Buffer == (uchar*)-1) {
590       if (ErrorStr) *ErrorStr = "Error mmapping file!";
591       close(FD); return failure<Module*>(0);
592     }
593
594     BytecodeParser Parser;
595     Result  = Parser.ParseBytecode(Buffer, Buffer+Length);
596
597     munmap((char*)Buffer, Length);
598     close(FD);
599     if (ErrorStr) *ErrorStr = Parser.getError();
600   } else {                              // Read from stdin
601     size_t FileSize = 0;
602     int BlockSize;
603     uchar Buffer[4096], *FileData = 0;
604     while ((BlockSize = read(0, Buffer, 4))) {
605       if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
606
607       FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
608       memcpy(FileData+FileSize, Buffer, BlockSize);
609       FileSize += BlockSize;
610     }
611
612     if (FileSize == 0) {
613       if (ErrorStr) *ErrorStr = "Standard Input empty!";
614       free(FileData); return failure<Module*>(0);
615     }
616
617 #define ALIGN_PTRS 1
618 #if ALIGN_PTRS
619     uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE, 
620                               MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
621     assert((Buf != (uchar*)-1) && "mmap returned error!");
622     memcpy(Buf, FileData, FileSize);
623     free(FileData);
624 #else
625     uchar *Buf = FileData;
626 #endif
627
628     BytecodeParser Parser;
629     Result = Parser.ParseBytecode(Buf, Buf+FileSize);
630
631 #if ALIGN_PTRS
632     munmap((char*)Buf, FileSize);   // Free mmap'd data area
633 #else
634     free(FileData);          // Free realloc'd block of memory
635 #endif
636
637     if (ErrorStr) *ErrorStr = Parser.getError();
638   }
639
640   return Result;
641 }