Changes to build successfully with GCC 3.02
[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/ConstantVals.h"
20 #include "llvm/iPHINode.h"
21 #include "llvm/iOther.h"
22 #include <sys/types.h>
23 typedef int blksize_t;
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <algorithm>
29 #include <iostream>
30 using std::cerr;
31 using std::make_pair;
32
33 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
34   if (Ty->isPrimitiveType()) {
35     Slot = Ty->getPrimitiveID();
36   } else {
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]);
43     } else {
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]);
47     }
48   }
49   //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
50   return false;
51 }
52
53 const Type *BytecodeParser::getType(unsigned ID) {
54   const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
55   if (T) return T;
56   
57   //cerr << "Looking up Type ID: " << ID << "\n";
58
59   const Value *D = getValue(Type::TypeTy, ID, false);
60   if (D == 0) return failure<const Type*>(0);
61
62   return cast<Type>(D);
63 }
64
65 int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
66   unsigned type;
67   if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
68   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
69  
70   if (ValueTab.size() <= type)
71     ValueTab.resize(type+1, ValueList());
72
73   //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
74   //     << "] = " << Val << "\n";
75   ValueTab[type].push_back(Val);
76
77   return ValueTab[type].size()-1;
78 }
79
80 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
81   unsigned Num = oNum;
82   unsigned type;   // The type plane it lives in...
83
84   if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
85
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...
90
91     // Otherwise, derived types need offset...
92     Num -= FirstDerivedTyID;
93
94     // Is it a module level type?
95     if (Num < ModuleTypeValues.size())
96       return (Value*)ModuleTypeValues[Num].get();
97
98     // Nope, is it a method level type?
99     Num -= ModuleTypeValues.size();
100     if (Num < MethodTypeValues.size())
101       return (Value*)MethodTypeValues[Num].get();
102
103     return 0;
104   }
105
106   if (type < ModuleValues.size()) {
107     if (Num < ModuleValues[type].size())
108       return ModuleValues[type][Num];
109     Num -= ModuleValues[type].size();
110   }
111
112   if (Values.size() > type && Values[type].size() > Num)
113     return Values[type][Num];
114
115   if (!Create) return failure<Value*>(0);  // Do not create a placeholder?
116
117   Value *d = 0;
118   switch (Ty->getPrimitiveID()) {
119   case Type::LabelTyID: d = new    BBPHolder(Ty, oNum); break;
120   case Type::MethodTyID:
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);
125     return d;
126   default:                   d = new   DefPHolder(Ty, oNum); break;
127   }
128
129   assert(d != 0 && "How did we not make something?");
130   if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
131   return d;
132 }
133
134 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
135   bool Error = false;
136   for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
137     ValueList &DL = ValTab[ty];
138     unsigned Size;
139     while ((Size = DL.size())) {
140       unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
141
142       Value *D = DL[Size-1];
143       DL.pop_back();
144
145       Value *NewDef = getValue(D->getType(), IDNumber, false);
146       if (NewDef == 0) {
147         Error = true;  // Unresolved thinger
148         cerr << "Unresolvable reference found: <"
149               << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
150       } else {
151         // Fixup all of the uses of this placeholder def...
152         D->replaceAllUsesWith(NewDef);
153
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!
157       }
158     }
159   }
160
161   return Error;
162 }
163
164 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf, 
165                                      BasicBlock *&BB) {
166   BB = new BasicBlock();
167
168   while (Buf < EndBuf) {
169     Instruction *Inst;
170     if (ParseInstruction(Buf, EndBuf, Inst)) {
171       delete BB;
172       return failure(true);
173     }
174
175     if (Inst == 0) { delete BB; return failure(true); }
176     if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
177
178     BB->getInstList().push_back(Inst);
179
180     BCR_TRACE(4, Inst);
181   }
182
183   return false;
184 }
185
186 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
187                                       SymbolTable *ST) {
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);
195
196     BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
197               " entries\n");
198
199     for (unsigned i = 0; i < NumEntries; ++i) {
200       // Symtab entry: [def slot #][name]
201       unsigned slot;
202       if (read_vbr(Buf, EndBuf, slot)) return failure(true);
203       std::string Name;
204       if (read(Buf, EndBuf, Name, false))  // Not aligned...
205         return failure(true);
206
207       Value *D = getValue(Ty, slot, false); // Find mapping...
208       if (D == 0) {
209         BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
210         return failure(true);
211       }
212       BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
213                 if (!isa<Instruction>(D)) cerr << "\n");
214
215       D->setName(Name, ST);
216     }
217   }
218
219   if (Buf > EndBuf) return failure(true);
220   return false;
221 }
222
223 // DeclareNewGlobalValue - Patch up forward references to global values in the
224 // form of ConstantPointerRef.
225 //
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));
230
231   if (I != GlobalRefs.end()) {
232     GlobalVariable *OldGV = I->second;   // Get the placeholder...
233     BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
234       
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");
242       
243       BCR_TRACE(4, "Mutating Forward Ref!\n");
244       
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);
248     }
249     
250     // Remove GV from the module...
251     GV->getParent()->getGlobalList().remove(OldGV);
252     delete OldGV;                        // Delete the old placeholder
253     
254     // Remove the map entry for the global now that it has been created...
255     GlobalRefs.erase(I);
256   }
257 }
258
259 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf, 
260                                  Module *C) {
261   // Clear out the local values table...
262   Values.clear();
263   if (MethodSignatureList.empty()) {
264     Error = "Method found, but MethodSignatureList empty!";
265     return failure(true);  // Unexpected method!
266   }
267
268   const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
269   const MethodType  *MTy  = dyn_cast<const MethodType>(PMTy->getElementType());
270   if (MTy == 0) return failure(true);  // Not ptr to method!
271
272   unsigned isInternal;
273   if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
274
275   unsigned MethSlot = MethodSignatureList.front().second;
276   MethodSignatureList.pop_front();
277   Method *M = new Method(MTy, isInternal != 0);
278
279   BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
280
281   const MethodType::ParamTypes &Params = MTy->getParamTypes();
282   for (MethodType::ParamTypes::const_iterator It = Params.begin();
283        It != Params.end(); ++It) {
284     MethodArgument *MA = new MethodArgument(*It);
285     if (insertValue(MA, Values) == -1) {
286       Error = "Error reading method arguments!\n";
287       delete M; return failure(true); 
288     }
289     M->getArgumentList().push_back(MA);
290   }
291
292   while (Buf < EndBuf) {
293     unsigned Type, Size;
294     const uchar *OldBuf = Buf;
295     if (readBlock(Buf, EndBuf, Type, Size)) {
296       Error = "Error reading Method level block!";
297       delete M; return failure(true); 
298     }
299
300     switch (Type) {
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);
305       }
306       break;
307
308     case BytecodeFormat::BasicBlock: {
309       BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
310       BasicBlock *BB;
311       if (ParseBasicBlock(Buf, Buf+Size, BB) ||
312           insertValue(BB, Values) == -1) {
313         delete M; return failure(true);                // Parse error... :(
314       }
315
316       M->getBasicBlocks().push_back(BB);
317       break;
318     }
319
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);
324       }
325       break;
326
327     default:
328       BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
329       Buf += Size;
330       if (OldBuf > Buf) return failure(true); // Wrap around!
331       break;
332     }
333     BCR_TRACE(2, "} end block\n");
334
335     if (align32(Buf, EndBuf)) {
336       Error = "Error aligning Method level block!";
337       delete M;    // Malformed bc file, read past end of block.
338       return failure(true);
339     }
340   }
341
342   if (postResolveValues(LateResolveValues) ||
343       postResolveValues(LateResolveModuleValues)) {
344     Error = "Error resolving method values!";
345     delete M; return failure(true);     // Unresolvable references!
346   }
347
348   Value *MethPHolder = getValue(PMTy, MethSlot, false);
349   assert(MethPHolder && "Something is broken no placeholder found!");
350   assert(isa<Method>(MethPHolder) && "Not a method?");
351
352   unsigned type;  // Type slot
353   assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
354   getTypeSlot(PMTy, type);
355
356   C->getMethodList().push_back(M);
357
358   // Replace placeholder with the real method pointer...
359   ModuleValues[type][MethSlot] = M;
360
361   // Clear out method level types...
362   MethodTypeValues.clear();
363
364   // If anyone is using the placeholder make them use the real method instead
365   MethPHolder->replaceAllUsesWith(M);
366
367   // We don't need the placeholder anymore!
368   delete MethPHolder;
369
370   // If the method is empty, we don't need the method argument entries...
371   if (M->isExternal())
372     M->getArgumentList().delete_all();
373
374   DeclareNewGlobalValue(M, MethSlot);
375
376   return false;
377 }
378
379 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
380                                            Module *Mod) {
381   if (!MethodSignatureList.empty()) {
382     Error = "Two ModuleGlobalInfo packets found!";
383     return failure(true);  // Two ModuleGlobal blocks?
384   }
385
386   // Read global variables...
387   unsigned VarType;
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); 
396     }
397
398     const PointerType *PTy = cast<const PointerType>(Ty);
399     const Type *ElTy = PTy->getElementType();
400
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.
405       //
406       unsigned InitSlot;
407       if (read_vbr(Buf, End, InitSlot)) return failure(true);
408       
409       Value *V = getValue(ElTy, InitSlot, false);
410       if (V == 0) return failure(true);
411       Initializer = cast<Constant>(V);
412     }
413
414     // Create the global variable...
415     GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
416                                             Initializer);
417     int DestSlot = insertValue(GV, ModuleValues);
418     if (DestSlot == -1) return failure(true);
419
420     Mod->getGlobalList().push_back(GV);
421
422     DeclareNewGlobalValue(GV, unsigned(DestSlot));
423
424     BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription() 
425               << " into slot #" << DestSlot << "\n");
426
427     if (read_vbr(Buf, End, VarType)) return failure(true);
428   }
429
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<MethodType>(cast<PointerType>(Ty)->getElementType())) { 
438       Error = "Method not ptr to meth type!  Ty = " + Ty->getDescription();
439       return failure(true); 
440     }
441     
442     // We create methods by passing the underlying MethodType to create...
443     Ty = cast<PointerType>(Ty)->getElementType();
444
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.
449
450     // Insert the placeholder...
451     Value *Val = new MethPHolder(Ty, 0);
452     if (insertValue(Val, ModuleValues) == -1) return failure(true);
453
454     // Figure out which entry of its typeslot it went into...
455     unsigned TypeSlot;
456     if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
457
458     unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
459     
460     // Keep track of this information in a linked list that is emptied as 
461     // methods are loaded...
462     //
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, "Method of type: " << Ty << "\n");
467   }
468
469   if (align32(Buf, End)) return failure(true);
470
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.
473   //
474   Buf = End;
475   return false;
476 }
477
478 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf, 
479                                 Module *&C) {
480
481   unsigned Type, Size;
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?
486   }
487
488   BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
489   MethodSignatureList.clear();                 // Just in case...
490
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");
495
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); }
500     switch (Type) {
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);
505       }
506       break;
507
508     case BytecodeFormat::ModuleGlobalInfo:
509       BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
510
511       if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
512         delete C; return failure(true);
513       }
514       break;
515
516     case BytecodeFormat::Method: {
517       BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
518       if (ParseMethod(Buf, Buf+Size, C)) {
519         delete C; return failure(true);               // Error parsing method
520       }
521       break;
522     }
523
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);
528       }
529       break;
530
531     default:
532       Error = "Expected Module Block!";
533       Buf += Size;
534       if (OldBuf > Buf) return failure(true); // Wrap around!
535       break;
536     }
537     BCR_TRACE(1, "} end block\n");
538     if (align32(Buf, EndBuf)) { delete C; return failure(true); }
539   }
540
541   if (!MethodSignatureList.empty()) {     // Expected more methods!
542     Error = "Method expected, but bytecode stream at end!";
543     return failure(true);
544   }
545
546   BCR_TRACE(0, "} end block\n\n");
547   return false;
548 }
549
550 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
551   LateResolveValues.clear();
552   unsigned Sig;
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!
558   }
559
560   Module *Result;
561   if (ParseModule(Buf, EndBuf, Result)) return 0;
562   return Result;
563 }
564
565
566 Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
567   BytecodeParser Parser;
568   return Parser.ParseBytecode(Buffer, Buffer+Length);
569 }
570
571 // Parse and return a class file...
572 //
573 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
574   struct stat StatBuf;
575   Module *Result = 0;
576
577   if (Filename != std::string("-")) {        // Read from a file...
578     int FD = open(Filename.c_str(), O_RDONLY);
579     if (FD == -1) {
580       if (ErrorStr) *ErrorStr = "Error opening file!";
581       return failure<Module*>(0);
582     }
583
584     if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
585
586     int Length = StatBuf.st_size;
587     if (Length == 0) { 
588       if (ErrorStr) *ErrorStr = "Error stat'ing file!";
589       close(FD); return failure<Module*>(0); 
590     }
591     uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ, 
592                                 MAP_PRIVATE, FD, 0);
593     if (Buffer == (uchar*)-1) {
594       if (ErrorStr) *ErrorStr = "Error mmapping file!";
595       close(FD); return failure<Module*>(0);
596     }
597
598     BytecodeParser Parser;
599     Result  = Parser.ParseBytecode(Buffer, Buffer+Length);
600
601     munmap((char*)Buffer, Length);
602     close(FD);
603     if (ErrorStr) *ErrorStr = Parser.getError();
604   } else {                              // Read from stdin
605     size_t FileSize = 0;
606     int BlockSize;
607     uchar Buffer[4096], *FileData = 0;
608     while ((BlockSize = read(0, Buffer, 4))) {
609       if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
610
611       FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
612       memcpy(FileData+FileSize, Buffer, BlockSize);
613       FileSize += BlockSize;
614     }
615
616     if (FileSize == 0) {
617       if (ErrorStr) *ErrorStr = "Standard Input empty!";
618       free(FileData); return failure<Module*>(0);
619     }
620
621 #define ALIGN_PTRS 1
622 #if ALIGN_PTRS
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);
627     free(FileData);
628 #else
629     uchar *Buf = FileData;
630 #endif
631
632     BytecodeParser Parser;
633     Result = Parser.ParseBytecode(Buf, Buf+FileSize);
634
635 #if ALIGN_PTRS
636     munmap((char*)Buf, FileSize);   // Free mmap'd data area
637 #else
638     free(FileData);          // Free realloc'd block of memory
639 #endif
640
641     if (ErrorStr) *ErrorStr = Parser.getError();
642   }
643
644   return Result;
645 }