blob: 540768383acaba9e4f96dabdfd02df4fb87e4d64 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- Reader.cpp - Code to read bytecode files -----------------------------===
2//
3// This library implements the functionality defined in llvm/Bytecode/Reader.h
4//
5// Note that this library should be as fast as possible, reentrant, and
6// threadsafe!!
7//
8// TODO: Make error message outputs be configurable depending on an option?
9// TODO: Allow passing in an option to ignore the symbol table
10//
11//===------------------------------------------------------------------------===
12
13#include "llvm/Bytecode/Reader.h"
14#include "llvm/Bytecode/Format.h"
15#include "llvm/Module.h"
16#include "llvm/BasicBlock.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/ConstPoolVals.h"
19#include "llvm/iOther.h"
20#include "ReaderInternals.h"
21#include <sys/types.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <algorithm>
27
28bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
29 if (Ty->isPrimitiveType()) {
30 Slot = Ty->getPrimitiveID();
31 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000032 // Check the method level types first...
33 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
34 MethodTypeValues.end(), Ty);
35 if (I != MethodTypeValues.end()) {
36 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
37 (&*I - &MethodTypeValues[0]);
38 } else {
39 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
40 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
41 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
42 }
Chris Lattner00950542001-06-06 20:29:01 +000043 }
44 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
45 return false;
46}
47
48const Type *BytecodeParser::getType(unsigned ID) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
50 if (T) return T;
51
52 //cerr << "Looking up Type ID: " << ID << endl;
53
54 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +000055 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000056
Chris Lattner1d670cc2001-09-07 16:37:43 +000057 return D->castTypeAsserting();
Chris Lattner00950542001-06-06 20:29:01 +000058}
59
Chris Lattner1d670cc2001-09-07 16:37:43 +000060bool BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000061 unsigned type;
Chris Lattner1d670cc2001-09-07 16:37:43 +000062 if (getTypeSlot(Val->getType(), type)) return failure(true);
63 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000064
65 if (ValueTab.size() <= type)
66 ValueTab.resize(type+1, ValueList());
67
68 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner1d670cc2001-09-07 16:37:43 +000069 // << "] = " << Val << endl;
70 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000071
72 return false;
73}
74
75Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
76 unsigned Num = oNum;
77 unsigned type; // The type plane it lives in...
78
Chris Lattner3d3f2892001-07-28 17:50:18 +000079 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000080
81 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000082 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000083 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
84 if (T) return (Value*)T; // Asked for a primitive type...
85
86 // Otherwise, derived types need offset...
87 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000088
89 // Is it a module level type?
90 if (Num < ModuleTypeValues.size())
91 return (Value*)(const Type*)ModuleTypeValues[Num];
92
93 // Nope, is it a method level type?
94 Num -= ModuleTypeValues.size();
95 if (Num < MethodTypeValues.size())
96 return (Value*)(const Type*)MethodTypeValues[Num];
97
98 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000099 }
100
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 if (type < ModuleValues.size()) {
102 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000103 return ModuleValues[type][Num];
104 Num -= ModuleValues[type].size();
105 }
106
107 if (Values.size() > type && Values[type].size() > Num)
108 return Values[type][Num];
109
Chris Lattner3d3f2892001-07-28 17:50:18 +0000110 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000111
112 Value *d = 0;
113 switch (Ty->getPrimitiveID()) {
114 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
115 case Type::MethodTyID:
116 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
117 << Ty->getName() << endl;
118 d = new MethPHolder(Ty, oNum);
119 insertValue(d, LateResolveModuleValues);
120 return d;
121 default: d = new DefPHolder(Ty, oNum); break;
122 }
123
124 assert(d != 0 && "How did we not make something?");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000125 if (insertValue(d, LateResolveValues)) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000126 return d;
127}
128
129bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
130 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000131 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000132 ValueList &DL = ValTab[ty];
133 unsigned Size;
134 while ((Size = DL.size())) {
135 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
136
137 Value *D = DL[Size-1];
138 DL.pop_back();
139
140 Value *NewDef = getValue(D->getType(), IDNumber, false);
141 if (NewDef == 0) {
142 Error = true; // Unresolved thinger
143 cerr << "Unresolvable reference found: <" << D->getType()->getName()
144 << ">:" << IDNumber << "!\n";
145 } else {
146 // Fixup all of the uses of this placeholder def...
147 D->replaceAllUsesWith(NewDef);
148
149 // Now that all the uses are gone, delete the placeholder...
150 // If we couldn't find a def (error case), then leak a little
151 delete D; // memory, 'cause otherwise we can't remove all uses!
152 }
153 }
154 }
155
156 return Error;
157}
158
159bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
160 BasicBlock *&BB) {
161 BB = new BasicBlock();
162
163 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000164 Instruction *Inst;
165 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000166 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000167 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169
Chris Lattner1d670cc2001-09-07 16:37:43 +0000170 if (Inst == 0) { delete BB; return failure(true); }
171 if (insertValue(Inst, Values)) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000172
Chris Lattner1d670cc2001-09-07 16:37:43 +0000173 BB->getInstList().push_back(Inst);
174
175 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000176 }
177
178 return false;
179}
180
Chris Lattner1d670cc2001-09-07 16:37:43 +0000181bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
182 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000183 while (Buf < EndBuf) {
184 // Symtab block header: [num entries][type id number]
185 unsigned NumEntries, Typ;
186 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000187 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000188 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000189 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner1d670cc2001-09-07 16:37:43 +0000191 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
192 " entries\n");
193
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000194 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000195 // Symtab entry: [def slot #][name]
196 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000197 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000198 string Name;
199 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000200 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000201
202 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000203 if (D == 0) {
204 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << endl);
205 return failure(true);
206 }
207 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
208 if (!D->isInstruction()) cerr << endl);
209
210 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000211 }
212 }
213
Chris Lattner3d3f2892001-07-28 17:50:18 +0000214 if (Buf > EndBuf) return failure(true);
215 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000216}
217
218
219bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
220 Module *C) {
221 // Clear out the local values table...
222 Values.clear();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000223 if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
Chris Lattner00950542001-06-06 20:29:01 +0000224
225 const MethodType *MTy = MethodSignatureList.front().first;
226 unsigned MethSlot = MethodSignatureList.front().second;
227 MethodSignatureList.pop_front();
228 Method *M = new Method(MTy);
229
Chris Lattner1d670cc2001-09-07 16:37:43 +0000230 BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
231
Chris Lattner00950542001-06-06 20:29:01 +0000232 const MethodType::ParamTypes &Params = MTy->getParamTypes();
233 for (MethodType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000234 It != Params.end(); ++It) {
Chris Lattner00950542001-06-06 20:29:01 +0000235 MethodArgument *MA = new MethodArgument(*It);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000236 if (insertValue(MA, Values)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000237 M->getArgumentList().push_back(MA);
238 }
239
240 while (Buf < EndBuf) {
241 unsigned Type, Size;
242 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000243 if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000244
245 switch (Type) {
246 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000247 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
248 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000249 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251 break;
252
253 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000254 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000255 BasicBlock *BB;
256 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
257 insertValue(BB, Values)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000258 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
260
261 M->getBasicBlocks().push_back(BB);
262 break;
263 }
264
265 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000266 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
267 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000268 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000269 }
270 break;
271
272 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000273 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000274 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000275 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000276 break;
277 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000278 BCR_TRACE(2, "} end block\n");
279
Chris Lattner00950542001-06-06 20:29:01 +0000280 if (align32(Buf, EndBuf)) {
281 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000282 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000283 }
284 }
285
286 if (postResolveValues(LateResolveValues) ||
287 postResolveValues(LateResolveModuleValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000288 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000289 }
290
291 Value *MethPHolder = getValue(MTy, MethSlot, false);
292 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000293 assert(MethPHolder->isMethod() && "Not a method?");
Chris Lattner00950542001-06-06 20:29:01 +0000294
295 unsigned type; // Type slot
296 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
297 getTypeSlot(MTy, type);
298
299 C->getMethodList().push_back(M);
300
301 // Replace placeholder with the real method pointer...
302 ModuleValues[type][MethSlot] = M;
303
304 // If anyone is using the placeholder make them use the real method instead
305 MethPHolder->replaceAllUsesWith(M);
306
307 // We don't need the placeholder anymore!
308 delete MethPHolder;
309
310 return false;
311}
312
313bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
314 Module *C) {
315
Chris Lattner3d3f2892001-07-28 17:50:18 +0000316 if (!MethodSignatureList.empty())
317 return failure(true); // Two ModuleGlobal blocks?
Chris Lattner00950542001-06-06 20:29:01 +0000318
319 // Read the method signatures for all of the methods that are coming, and
320 // create fillers in the Value tables.
321 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000322 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000323 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
324 const Type *Ty = getType(MethSignature);
325 if (!Ty || !Ty->isMethodType()) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000326 cerr << "Method not meth type! Ty = " << Ty << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000327 if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000328 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000329 }
330
Chris Lattner1d670cc2001-09-07 16:37:43 +0000331 // When the ModuleGlobalInfo section is read, we load the type of each
332 // method and the 'ModuleValues' slot that it lands in. We then load a
333 // placeholder into its slot to reserve it. When the method is loaded, this
334 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000335
336 // Insert the placeholder...
337 Value *Def = new MethPHolder(Ty, 0);
338 insertValue(Def, ModuleValues);
339
340 // Figure out which entry of its typeslot it went into...
341 unsigned TypeSlot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000342 if (getTypeSlot(Def->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000343
344 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
345
346 // Keep track of this information in a linked list that is emptied as
347 // methods are loaded...
348 //
349 MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000350 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000351 BCR_TRACE(2, "Method of type: " << Ty << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000352 }
353
Chris Lattner3d3f2892001-07-28 17:50:18 +0000354 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000355
356 // This is for future proofing... in the future extra fields may be added that
357 // we don't understand, so we transparently ignore them.
358 //
359 Buf = End;
360 return false;
361}
362
363bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
364 Module *&C) {
365
366 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000367 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000368 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
Chris Lattner3d3f2892001-07-28 17:50:18 +0000369 return failure(true); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000370
Chris Lattner1d670cc2001-09-07 16:37:43 +0000371 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000372 MethodSignatureList.clear(); // Just in case...
373
374 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000375 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
376 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000377 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000378
379 C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000380 while (Buf < EndBuf) {
381 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000382 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000383 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000384 case BytecodeFormat::ConstantPool:
385 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
386 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000387 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000388 }
389 break;
390
Chris Lattner1d670cc2001-09-07 16:37:43 +0000391 case BytecodeFormat::ModuleGlobalInfo:
392 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
393
394 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000395 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000396 }
397 break;
398
399 case BytecodeFormat::Method: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000400 BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000401 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000402 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000403 }
404 break;
405 }
406
407 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000408 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
409 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000410 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000411 }
412 break;
413
414 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000415 cerr << " Unknown class block: " << Type << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000416 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000417 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000418 break;
419 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000420 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000421 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000422 }
423
424 if (!MethodSignatureList.empty()) // Expected more methods!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000425 return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000426
427 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000428 return false;
429}
430
431Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
432 LateResolveValues.clear();
433 unsigned Sig;
434 // Read and check signature...
435 if (read(Buf, EndBuf, Sig) ||
436 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000437 return failure<Module*>(0); // Invalid signature!
Chris Lattner00950542001-06-06 20:29:01 +0000438
439 Module *Result;
440 if (ParseModule(Buf, EndBuf, Result)) return 0;
441 return Result;
442}
443
444
445Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
446 BytecodeParser Parser;
447 return Parser.ParseBytecode(Buffer, Buffer+Length);
448}
449
450// Parse and return a class file...
451//
452Module *ParseBytecodeFile(const string &Filename) {
453 struct stat StatBuf;
454 Module *Result = 0;
455
456 if (Filename != string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000457 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000458 if (FD == -1) return failure<Module*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000459
Chris Lattner3d3f2892001-07-28 17:50:18 +0000460 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000461
462 int Length = StatBuf.st_size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000463 if (Length == 0) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000464 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
465 MAP_PRIVATE, FD, 0);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000466 if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000467
468 BytecodeParser Parser;
469 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
470
471 munmap((char*)Buffer, Length);
472 close(FD);
473 } else { // Read from stdin
474 size_t FileSize = 0;
475 int BlockSize;
476 uchar Buffer[4096], *FileData = 0;
477 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000478 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000479
480 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
481 memcpy(FileData+FileSize, Buffer, BlockSize);
482 FileSize += BlockSize;
483 }
484
Chris Lattner3d3f2892001-07-28 17:50:18 +0000485 if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000486
487#define ALIGN_PTRS 1
488#if ALIGN_PTRS
489 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
490 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
491 assert((Buf != (uchar*)-1) && "mmap returned error!");
492 free(FileData);
493 memcpy(Buf, FileData, FileSize);
494#else
495 uchar *Buf = FileData;
496#endif
497
498 BytecodeParser Parser;
499 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
500
501#if ALIGN_PTRS
502 munmap((char*)Buf, FileSize); // Free mmap'd data area
503#else
504 free(FileData); // Free realloc'd block of memory
505#endif
506 }
507
508 return Result;
509}