blob: 48e14dd7ced324aa0bf8230191e71c2256a4e2c7 [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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//
Chris Lattner74734132002-08-17 22:01:27 +00008// TODO: Return error messages to caller instead of printing them out directly.
Chris Lattner00950542001-06-06 20:29:01 +00009// TODO: Allow passing in an option to ignore the symbol table
10//
Chris Lattnerd6b65252001-10-24 01:15:12 +000011//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000012
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Bytecode/Reader.h"
15#include "llvm/Bytecode/Format.h"
16#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000021#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <fcntl.h>
24#include <unistd.h>
25#include <algorithm>
26
27bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
28 if (Ty->isPrimitiveType()) {
29 Slot = Ty->getPrimitiveID();
30 } else {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000031 // Check the function level types first...
Chris Lattner6e5a0e42003-03-06 17:18:14 +000032 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
33 FunctionTypeValues.end(), Ty);
34 if (I != FunctionTypeValues.end()) {
Chris Lattner1d670cc2001-09-07 16:37:43 +000035 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
Chris Lattner6e5a0e42003-03-06 17:18:14 +000036 (&*I - &FunctionTypeValues[0]);
Chris Lattner1d670cc2001-09-07 16:37:43 +000037 } else {
38 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
39 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
40 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
41 }
Chris Lattner00950542001-06-06 20:29:01 +000042 }
Chris Lattner697954c2002-01-20 22:54:45 +000043 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000044 return false;
45}
46
47const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000048 if (ID < Type::NumPrimitiveIDs) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
50 if (T) return T;
51 }
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattner697954c2002-01-20 22:54:45 +000053 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000054 const Value *V = getValue(Type::TypeTy, ID, false);
55 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
Chris Lattner697954c2002-01-20 22:54:45 +000058int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000059 unsigned type;
Chris Lattner74734132002-08-17 22:01:27 +000060 if (getTypeSlot(Val->getType(), type)) return -1;
Chris Lattner1d670cc2001-09-07 16:37:43 +000061 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000062
Chris Lattner036b8aa2003-03-06 17:55:45 +000063 while (ValueTab.size() <= type) {
64 ValueTab.push_back(ValueList());
65 if (HasImplicitZeroInitializer) // add a zero initializer if appropriate
66 ValueTab.back().push_back(
67 Constant::getNullValue(getType(ValueTab.size()-1)));
68 }
Chris Lattner00950542001-06-06 20:29:01 +000069
70 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000071 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattner05950c32001-10-13 06:47:01 +000074 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000075}
76
77Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
78 unsigned Num = oNum;
79 unsigned type; // The type plane it lives in...
80
Chris Lattner74734132002-08-17 22:01:27 +000081 if (getTypeSlot(Ty, type)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000082
83 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000084 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +000085 if (Num < Type::NumPrimitiveIDs) {
86 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
87 if (T) return (Value*)T; // Asked for a primitive type...
88 }
Chris Lattner00950542001-06-06 20:29:01 +000089
90 // Otherwise, derived types need offset...
91 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000092
93 // Is it a module level type?
94 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000095 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000096
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000097 // Nope, is it a function level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +000098 Num -= ModuleTypeValues.size();
Chris Lattner6e5a0e42003-03-06 17:18:14 +000099 if (Num < FunctionTypeValues.size())
100 return (Value*)FunctionTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101
102 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000103 }
104
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 if (type < ModuleValues.size()) {
106 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000107 return ModuleValues[type][Num];
108 Num -= ModuleValues[type].size();
109 }
110
111 if (Values.size() > type && Values[type].size() > Num)
112 return Values[type][Num];
113
Chris Lattner74734132002-08-17 22:01:27 +0000114 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000115
116 Value *d = 0;
117 switch (Ty->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000118 case Type::FunctionTyID:
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000119 std::cerr << "Creating function pholder! : " << type << ":" << oNum << " "
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000120 << Ty->getName() << "\n";
Chris Lattner74734132002-08-17 22:01:27 +0000121 d = new FunctionPHolder(Ty, oNum);
122 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000123 return d;
Chris Lattner74734132002-08-17 22:01:27 +0000124 case Type::LabelTyID:
125 d = new BBPHolder(Ty, oNum);
126 break;
127 default:
128 d = new ValPHolder(Ty, oNum);
129 break;
Chris Lattner00950542001-06-06 20:29:01 +0000130 }
131
132 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000133 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000134 return d;
135}
136
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000137/// getConstantValue - Just like getValue, except that it returns a null pointer
138/// only on error. It always returns a constant (meaning that if the value is
139/// defined, but is not a constant, that is an error). If the specified
140/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
141/// after the real value is parsed, the placeholder is eliminated.
142///
143Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
144 if (Value *V = getValue(Ty, Slot, false))
145 return dyn_cast<Constant>(V); // If we already have the value parsed...
146
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000147 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(Ty, Slot));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000148 if (I != GlobalRefs.end()) {
149 BCR_TRACE(5, "Previous forward ref found!\n");
150 return cast<Constant>(I->second);
151 } else {
152 // Create a placeholder for the constant reference and
153 // keep track of the fact that we have a forward ref to recycle it
154 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
155 Constant *C = new ConstPHolder(Ty, Slot);
156
157 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000158 GlobalRefs.insert(std::make_pair(std::make_pair(Ty, Slot), C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000159 return C;
160 }
161}
162
163
164
Chris Lattner00950542001-06-06 20:29:01 +0000165bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
166 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000167 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000168 ValueList &DL = ValTab[ty];
169 unsigned Size;
170 while ((Size = DL.size())) {
171 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
172
173 Value *D = DL[Size-1];
174 DL.pop_back();
175
176 Value *NewDef = getValue(D->getType(), IDNumber, false);
177 if (NewDef == 0) {
178 Error = true; // Unresolved thinger
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000179 std::cerr << "Unresolvable reference found: <"
180 << D->getType()->getDescription() << ">:" << IDNumber <<"!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000181 } else {
182 // Fixup all of the uses of this placeholder def...
183 D->replaceAllUsesWith(NewDef);
184
185 // Now that all the uses are gone, delete the placeholder...
186 // If we couldn't find a def (error case), then leak a little
187 delete D; // memory, 'cause otherwise we can't remove all uses!
188 }
189 }
190 }
191
192 return Error;
193}
194
195bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
196 BasicBlock *&BB) {
197 BB = new BasicBlock();
198
199 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000200 Instruction *Inst;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000201 if (ParseInstruction(Buf, EndBuf, Inst, /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000202 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000203 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000204 }
205
Chris Lattner74734132002-08-17 22:01:27 +0000206 if (Inst == 0) { delete BB; return true; }
207 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner1d670cc2001-09-07 16:37:43 +0000209 BB->getInstList().push_back(Inst);
210
211 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000212 }
213
214 return false;
215}
216
Chris Lattner1d670cc2001-09-07 16:37:43 +0000217bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
218 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000219 while (Buf < EndBuf) {
220 // Symtab block header: [num entries][type id number]
221 unsigned NumEntries, Typ;
222 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000223 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000224 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000225 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000226
Chris Lattner1d670cc2001-09-07 16:37:43 +0000227 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
228 " entries\n");
229
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000230 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000231 // Symtab entry: [def slot #][name]
232 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000233 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000234 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000235 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000236 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000237
238 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000239 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000240 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000241 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000242 }
243 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000244 if (!isa<Instruction>(D)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000245
246 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000247 }
248 }
249
Chris Lattner74734132002-08-17 22:01:27 +0000250 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000251 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000252}
253
Chris Lattner74734132002-08-17 22:01:27 +0000254void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000255 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
256 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000257 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000258
Chris Lattner74734132002-08-17 22:01:27 +0000259 BCR_TRACE(3, "Mutating forward refs!\n");
260 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000261
Chris Lattner74734132002-08-17 22:01:27 +0000262 // Loop over all of the uses of the Value. What they are depends
263 // on what NewV is. Replacing a use of the old reference takes the
264 // use off the use list, so loop with !use_empty(), not the use_iterator.
265 while (!VPH->use_empty()) {
266 Constant *C = cast<Constant>(VPH->use_back());
267 unsigned numReplaced = C->mutateReferences(VPH, NewV);
268 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000269
Chris Lattner74734132002-08-17 22:01:27 +0000270 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
271 // Remove the placeholder GlobalValue from the module...
272 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000273 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000274 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000275
Chris Lattner74734132002-08-17 22:01:27 +0000276 delete VPH; // Delete the old placeholder
277 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000278}
279
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000280bool BytecodeParser::ParseFunction(const uchar *&Buf, const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000281 // Clear out the local values table...
282 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000283 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000284 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000285 return true; // Unexpected function!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000286 }
Chris Lattner00950542001-06-06 20:29:01 +0000287
Chris Lattner74734132002-08-17 22:01:27 +0000288 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000289 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000290 if (MTy == 0) return true; // Not ptr to function!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000291
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000292 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000293 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000294
Chris Lattner74734132002-08-17 22:01:27 +0000295 unsigned MethSlot = FunctionSignatureList.back().second;
296 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000297 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000298
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000299 BCR_TRACE(2, "FUNCTION TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000301 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000302 Function::aiterator AI = M->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000303 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000304 It != Params.end(); ++It, ++AI) {
305 if (insertValue(AI, Values) == -1) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000306 Error = "Error reading function arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000307 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000308 }
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
310
311 while (Buf < EndBuf) {
312 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000313 const unsigned char *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000314 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000315 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000316 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000317 }
Chris Lattner00950542001-06-06 20:29:01 +0000318
319 switch (Type) {
320 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000321 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000322 if (ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000323 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000324 }
325 break;
326
327 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000328 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000329 BasicBlock *BB;
330 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000331 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000332 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000333 }
334
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000335 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000336 break;
337 }
338
339 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000340 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000341 if (ParseSymbolTable(Buf, Buf+Size, &M->getSymbolTable())) {
Chris Lattner74734132002-08-17 22:01:27 +0000342 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000343 }
344 break;
345
346 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000347 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000348 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000349 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000350 break;
351 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000352 BCR_TRACE(2, "} end block\n");
353
Chris Lattner00950542001-06-06 20:29:01 +0000354 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000355 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000356 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000357 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000358 }
359 }
360
361 if (postResolveValues(LateResolveValues) ||
362 postResolveValues(LateResolveModuleValues)) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000363 Error = "Error resolving function values!";
Chris Lattner74734132002-08-17 22:01:27 +0000364 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
366
Chris Lattner74734132002-08-17 22:01:27 +0000367 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000368 assert(FunctionPHolder && "Something is broken, no placeholder found!");
Chris Lattner74734132002-08-17 22:01:27 +0000369 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000370
371 unsigned type; // Type slot
372 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000373 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000374
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000375 TheModule->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000376
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000377 // Replace placeholder with the real function pointer...
Chris Lattner00950542001-06-06 20:29:01 +0000378 ModuleValues[type][MethSlot] = M;
379
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000380 // Clear out function level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000381 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000382
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000383 // If anyone is using the placeholder make them use the real function instead
Chris Lattner74734132002-08-17 22:01:27 +0000384 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000387 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000388
Chris Lattner74734132002-08-17 22:01:27 +0000389 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000390
Chris Lattner00950542001-06-06 20:29:01 +0000391 return false;
392}
393
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000394bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
Chris Lattner74734132002-08-17 22:01:27 +0000395 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000396 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000397 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000398 }
Chris Lattner00950542001-06-06 20:29:01 +0000399
Chris Lattner70cc3392001-09-10 07:58:01 +0000400 // Read global variables...
401 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000402 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000403 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000404 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
405 // bit2 = isInternal, bit3+ = slot#
406 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000407 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000408 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000409 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000410 }
411
Chris Lattneref9c23f2001-10-03 14:53:21 +0000412 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000413 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000414
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000415 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000416 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;
Chris Lattner74734132002-08-17 22:01:27 +0000421 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000422
Chris Lattner05950c32001-10-13 06:47:01 +0000423 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000424 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000425 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000426 }
427
Chris Lattner70cc3392001-09-10 07:58:01 +0000428 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000429 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
430 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000431 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000432 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000433
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000434 TheModule->getGlobalList().push_back(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000435
Chris Lattner74734132002-08-17 22:01:27 +0000436 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000437
438 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000439 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000440
Chris Lattner74734132002-08-17 22:01:27 +0000441 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000442 }
443
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000444 // Read the function signatures for all of the functions that are coming, and
Chris Lattner00950542001-06-06 20:29:01 +0000445 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000446 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);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000450 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000451 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
452 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000453 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000454 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000455
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000456 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000457 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000458
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000459 // When the ModuleGlobalInfo section is read, we load the type of each
460 // function and the 'ModuleValues' slot that it lands in. We then load a
461 // placeholder into its slot to reserve it. When the function is loaded,
462 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000463
464 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000465 Value *Val = new FunctionPHolder(Ty, 0);
466 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000467
468 // Figure out which entry of its typeslot it went into...
469 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000470 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000471
472 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
473
474 // Keep track of this information in a linked list that is emptied as
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000475 // functions are loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000476 //
Chris Lattner74734132002-08-17 22:01:27 +0000477 FunctionSignatureList.push_back(
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000478 std::make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000479 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000480 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000481 }
482
Chris Lattner74734132002-08-17 22:01:27 +0000483 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());
Chris Lattner00950542001-06-06 20:29:01 +0000488
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
Chris Lattner036b8aa2003-03-06 17:55:45 +0000496bool BytecodeParser::ParseVersionInfo(const uchar *&Buf, const uchar *EndBuf) {
497 unsigned Version;
498 if (read_vbr(Buf, EndBuf, Version)) return true;
499
500 // Unpack version number: low four bits are for flags, top bits = version
501 isBigEndian = Version & 1;
502 hasLongPointers = Version & 2;
503 RevisionNum = Version >> 4;
504 HasImplicitZeroInitializer = true;
505
506 switch (RevisionNum) {
507 case 0: // Initial revision
508 if (Version != 14) return true; // Unknown revision 0 flags?
509 FirstDerivedTyID = 14;
510 HasImplicitZeroInitializer = false;
511 isBigEndian = hasLongPointers = true;
512 break;
513 case 1:
514 FirstDerivedTyID = 14;
515 break;
516 default:
517 Error = "Unknown bytecode version number!";
518 return true;
519 }
520
521 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
522 BCR_TRACE(1, "BigEndian/LongPointers = " << isBigEndian << ","
523 << hasLongPointers << "\n");
524 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
525 return false;
526}
527
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000528bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000529 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000530 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000531 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
532 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000533 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000534 }
Chris Lattner00950542001-06-06 20:29:01 +0000535
Chris Lattner1d670cc2001-09-07 16:37:43 +0000536 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000537 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000538
539 // Read into instance variables...
Chris Lattner036b8aa2003-03-06 17:55:45 +0000540 if (ParseVersionInfo(Buf, EndBuf)) return true;
Chris Lattner74734132002-08-17 22:01:27 +0000541 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000542
Chris Lattner00950542001-06-06 20:29:01 +0000543 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000544 const unsigned char *OldBuf = Buf;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000545 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000546 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000547 case BytecodeFormat::ConstantPool:
548 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000549 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues))
550 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000551 break;
552
Chris Lattner1d670cc2001-09-07 16:37:43 +0000553 case BytecodeFormat::ModuleGlobalInfo:
554 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000555 if (ParseModuleGlobalInfo(Buf, Buf+Size)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000556 break;
557
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000558 case BytecodeFormat::Function: {
559 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000560 if (ParseFunction(Buf, Buf+Size)) return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000561 break;
562 }
563
564 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000565 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000566 if (ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()))
567 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000568 break;
569
570 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000571 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000572 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000573 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000574 break;
575 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000576 BCR_TRACE(1, "} end block\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000577 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000578 }
579
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000580 if (!FunctionSignatureList.empty()) { // Expected more functions!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000581 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000582 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000583 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000584
585 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000586 return false;
587}
588
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000589static inline Module *Error(std::string *ErrorStr, const char *Message) {
590 if (ErrorStr) *ErrorStr = Message;
591 return 0;
592}
593
Chris Lattner00950542001-06-06 20:29:01 +0000594Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
595 LateResolveValues.clear();
596 unsigned Sig;
597 // Read and check signature...
598 if (read(Buf, EndBuf, Sig) ||
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000599 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
600 return ::Error(&Error, "Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000601
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000602 TheModule = new Module();
603 if (ParseModule(Buf, EndBuf)) {
604 delete TheModule;
605 TheModule = 0;
606 }
607 return TheModule;
Chris Lattner00950542001-06-06 20:29:01 +0000608}
609
610
Chris Lattner09abe6a2003-03-06 16:50:32 +0000611Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
612 std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000613 BytecodeParser Parser;
Chris Lattner09abe6a2003-03-06 16:50:32 +0000614 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length);
615 if (ErrorStr) *ErrorStr = Parser.getError();
616 return R;
Chris Lattner00950542001-06-06 20:29:01 +0000617}
618
Chris Lattnerb6c46952003-03-06 17:03:28 +0000619
620/// FDHandle - Simple handle class to make sure a file descriptor gets closed
621/// when the object is destroyed.
622class FDHandle {
623 int FD;
624public:
625 FDHandle(int fd) : FD(fd) {}
626 operator int() const { return FD; }
627 ~FDHandle() {
628 if (FD != -1) close(FD);
629 }
630};
631
Chris Lattner00950542001-06-06 20:29:01 +0000632// Parse and return a class file...
633//
Chris Lattner697954c2002-01-20 22:54:45 +0000634Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000635 Module *Result = 0;
636
Chris Lattner697954c2002-01-20 22:54:45 +0000637 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb6c46952003-03-06 17:03:28 +0000638 FDHandle FD = open(Filename.c_str(), O_RDONLY);
639 if (FD == -1)
640 return Error(ErrorStr, "Error opening file!");
Chris Lattner00950542001-06-06 20:29:01 +0000641
Chris Lattnerb6c46952003-03-06 17:03:28 +0000642 // Stat the file to get its length...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000643 struct stat StatBuf;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000644 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
645 return Error(ErrorStr, "Error stat'ing file!");
Chris Lattner09abe6a2003-03-06 16:50:32 +0000646
Chris Lattnerb6c46952003-03-06 17:03:28 +0000647 // mmap in the file all at once...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000648 int Length = StatBuf.st_size;
649 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
650 MAP_PRIVATE, FD, 0);
Chris Lattnerb6c46952003-03-06 17:03:28 +0000651 if (Buffer == (unsigned char*)MAP_FAILED)
652 return Error(ErrorStr, "Error mmapping file!");
Chris Lattner00950542001-06-06 20:29:01 +0000653
Chris Lattnerb6c46952003-03-06 17:03:28 +0000654 // Parse the bytecode we mmapped in
Chris Lattner09abe6a2003-03-06 16:50:32 +0000655 Result = ParseBytecodeBuffer(Buffer, Length, ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000656
Chris Lattnerb6c46952003-03-06 17:03:28 +0000657 // Unmmap the bytecode...
Chris Lattner00950542001-06-06 20:29:01 +0000658 munmap((char*)Buffer, Length);
Chris Lattner00950542001-06-06 20:29:01 +0000659 } else { // Read from stdin
Chris Lattner00950542001-06-06 20:29:01 +0000660 int BlockSize;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000661 uchar Buffer[4096*4];
662 std::vector<unsigned char> FileData;
Chris Lattner00950542001-06-06 20:29:01 +0000663
Chris Lattnerb6c46952003-03-06 17:03:28 +0000664 // Read in all of the data from stdin, we cannot mmap stdin...
665 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
666 if (BlockSize == -1)
667 return Error(ErrorStr, "Error reading from stdin!");
668
669 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
Chris Lattner00950542001-06-06 20:29:01 +0000670 }
671
Chris Lattnerb6c46952003-03-06 17:03:28 +0000672 if (FileData.empty())
673 return Error(ErrorStr, "Standard Input empty!");
Chris Lattner00950542001-06-06 20:29:01 +0000674
Chris Lattner915ce8a2002-08-18 00:38:32 +0000675#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000676#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000677 uchar *Buf = (uchar*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
Chris Lattner00950542001-06-06 20:29:01 +0000678 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
679 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattnerb6c46952003-03-06 17:03:28 +0000680 memcpy(Buf, &FileData[0], FileData.size());
Chris Lattner00950542001-06-06 20:29:01 +0000681#else
Chris Lattnerb6c46952003-03-06 17:03:28 +0000682 unsigned char *Buf = &FileData[0];
Chris Lattner00950542001-06-06 20:29:01 +0000683#endif
684
Chris Lattnerb6c46952003-03-06 17:03:28 +0000685 Result = ParseBytecodeBuffer(Buf, FileData.size(), ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000686
687#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000688 munmap((char*)Buf, FileData.size()); // Free mmap'd data area
Chris Lattner00950542001-06-06 20:29:01 +0000689#endif
Chris Lattner00950542001-06-06 20:29:01 +0000690 }
691
692 return Result;
693}