blob: b7904bb60f797d20a5c729ac545bb284913acc80 [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"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
17#include "llvm/BasicBlock.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/ConstPoolVals.h"
20#include "llvm/iOther.h"
21#include "ReaderInternals.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
29bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
30 if (Ty->isPrimitiveType()) {
31 Slot = Ty->getPrimitiveID();
32 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000033 // 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 }
Chris Lattner00950542001-06-06 20:29:01 +000044 }
45 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
46 return false;
47}
48
49const 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);
Chris Lattner3d3f2892001-07-28 17:50:18 +000056 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000057
Chris Lattnercfe26c92001-10-01 18:26:53 +000058 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000059}
60
Chris Lattner1d670cc2001-09-07 16:37:43 +000061bool BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000062 unsigned type;
Chris Lattner1d670cc2001-09-07 16:37:43 +000063 if (getTypeSlot(Val->getType(), type)) return failure(true);
64 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000065
66 if (ValueTab.size() <= type)
67 ValueTab.resize(type+1, ValueList());
68
69 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner1d670cc2001-09-07 16:37:43 +000070 // << "] = " << Val << endl;
71 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000072
73 return false;
74}
75
76Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
77 unsigned Num = oNum;
78 unsigned type; // The type plane it lives in...
79
Chris Lattner3d3f2892001-07-28 17:50:18 +000080 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000081
82 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000083 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000084 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;
Chris Lattner1d670cc2001-09-07 16:37:43 +000089
90 // Is it a module level type?
91 if (Num < ModuleTypeValues.size())
92 return (Value*)(const Type*)ModuleTypeValues[Num];
93
94 // Nope, is it a method level type?
95 Num -= ModuleTypeValues.size();
96 if (Num < MethodTypeValues.size())
97 return (Value*)(const Type*)MethodTypeValues[Num];
98
99 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000100 }
101
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102 if (type < ModuleValues.size()) {
103 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000104 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
Chris Lattner3d3f2892001-07-28 17:50:18 +0000111 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000112
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 insertValue(d, LateResolveModuleValues);
121 return d;
122 default: d = new DefPHolder(Ty, oNum); break;
123 }
124
125 assert(d != 0 && "How did we not make something?");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000126 if (insertValue(d, LateResolveValues)) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000127 return d;
128}
129
130bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
131 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000132 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000133 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: <" << D->getType()->getName()
145 << ">:" << 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
160bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
161 BasicBlock *&BB) {
162 BB = new BasicBlock();
163
164 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000165 Instruction *Inst;
166 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000167 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000168 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000169 }
170
Chris Lattner1d670cc2001-09-07 16:37:43 +0000171 if (Inst == 0) { delete BB; return failure(true); }
172 if (insertValue(Inst, Values)) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000173
Chris Lattner1d670cc2001-09-07 16:37:43 +0000174 BB->getInstList().push_back(Inst);
175
176 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000177 }
178
179 return false;
180}
181
Chris Lattner1d670cc2001-09-07 16:37:43 +0000182bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
183 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000184 while (Buf < EndBuf) {
185 // Symtab block header: [num entries][type id number]
186 unsigned NumEntries, Typ;
187 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000188 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000189 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000190 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000191
Chris Lattner1d670cc2001-09-07 16:37:43 +0000192 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
193 " entries\n");
194
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000195 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000196 // Symtab entry: [def slot #][name]
197 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000198 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000199 string Name;
200 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000201 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000202
203 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000204 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 (!D->isInstruction()) cerr << endl);
210
211 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000212 }
213 }
214
Chris Lattner3d3f2892001-07-28 17:50:18 +0000215 if (Buf > EndBuf) return failure(true);
216 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000217}
218
219
220bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
221 Module *C) {
222 // Clear out the local values table...
223 Values.clear();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000224 if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
Chris Lattner00950542001-06-06 20:29:01 +0000225
226 const MethodType *MTy = MethodSignatureList.front().first;
227 unsigned MethSlot = MethodSignatureList.front().second;
228 MethodSignatureList.pop_front();
229 Method *M = new Method(MTy);
230
Chris Lattner1d670cc2001-09-07 16:37:43 +0000231 BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
232
Chris Lattner00950542001-06-06 20:29:01 +0000233 const MethodType::ParamTypes &Params = MTy->getParamTypes();
234 for (MethodType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000235 It != Params.end(); ++It) {
Chris Lattner00950542001-06-06 20:29:01 +0000236 MethodArgument *MA = new MethodArgument(*It);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000237 if (insertValue(MA, Values)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000238 M->getArgumentList().push_back(MA);
239 }
240
241 while (Buf < EndBuf) {
242 unsigned Type, Size;
243 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000245
246 switch (Type) {
247 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000248 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
249 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000250 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
252 break;
253
254 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000255 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000256 BasicBlock *BB;
257 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
258 insertValue(BB, Values)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000259 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
261
262 M->getBasicBlocks().push_back(BB);
263 break;
264 }
265
266 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000267 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
268 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000269 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000270 }
271 break;
272
273 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000274 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000275 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000276 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000277 break;
278 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000279 BCR_TRACE(2, "} end block\n");
280
Chris Lattner00950542001-06-06 20:29:01 +0000281 if (align32(Buf, EndBuf)) {
282 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000283 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000284 }
285 }
286
287 if (postResolveValues(LateResolveValues) ||
288 postResolveValues(LateResolveModuleValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000289 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000290 }
291
292 Value *MethPHolder = getValue(MTy, MethSlot, false);
293 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000294 assert(MethPHolder->isMethod() && "Not a method?");
Chris Lattner00950542001-06-06 20:29:01 +0000295
296 unsigned type; // Type slot
297 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
298 getTypeSlot(MTy, type);
299
300 C->getMethodList().push_back(M);
301
302 // Replace placeholder with the real method pointer...
303 ModuleValues[type][MethSlot] = M;
304
Chris Lattnere4d71a12001-09-14 22:03:42 +0000305 // Clear out method level types...
306 MethodTypeValues.clear();
307
Chris Lattner00950542001-06-06 20:29:01 +0000308 // If anyone is using the placeholder make them use the real method instead
309 MethPHolder->replaceAllUsesWith(M);
310
311 // We don't need the placeholder anymore!
312 delete MethPHolder;
313
314 return false;
315}
316
317bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
318 Module *C) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000319 if (!MethodSignatureList.empty())
320 return failure(true); // Two ModuleGlobal blocks?
Chris Lattner00950542001-06-06 20:29:01 +0000321
Chris Lattner70cc3392001-09-10 07:58:01 +0000322 // Read global variables...
323 unsigned VarType;
324 if (read_vbr(Buf, End, VarType)) return failure(true);
325 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd70684f2001-09-18 04:01:05 +0000326 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
327 const Type *Ty = getType(VarType >> 2);
Chris Lattner70cc3392001-09-10 07:58:01 +0000328 if (!Ty || !Ty->isPointerType()) {
329 cerr << "Global not pointer type! Ty = " << Ty << endl;
330 return failure(true);
331 }
332
Chris Lattnerd70684f2001-09-18 04:01:05 +0000333 ConstPoolVal *Initializer = 0;
334 if (VarType & 2) { // Does it have an initalizer?
335 // Do not improvise... values must have been stored in the constant pool,
336 // which should have been read before now.
337 //
338 unsigned InitSlot;
339 if (read_vbr(Buf, End, InitSlot)) return failure(true);
340
341 Value *V = getValue(Ty->castPointerType()->getValueType(),
342 InitSlot, false);
343 if (V == 0) return failure(true);
Chris Lattnercfe26c92001-10-01 18:26:53 +0000344 Initializer = cast<ConstPoolVal>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000345 }
346
Chris Lattner70cc3392001-09-10 07:58:01 +0000347 // Create the global variable...
Chris Lattnerd70684f2001-09-18 04:01:05 +0000348 GlobalVariable *GV = new GlobalVariable(Ty, VarType & 1, Initializer);
Chris Lattner70cc3392001-09-10 07:58:01 +0000349 insertValue(GV, ModuleValues);
350 C->getGlobalList().push_back(GV);
351
352 if (read_vbr(Buf, End, VarType)) return failure(true);
353 BCR_TRACE(2, "Global Variable of type: " << Ty->getDescription() << endl);
354 }
355
Chris Lattner00950542001-06-06 20:29:01 +0000356 // Read the method signatures for all of the methods that are coming, and
357 // create fillers in the Value tables.
358 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000359 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000360 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
361 const Type *Ty = getType(MethSignature);
362 if (!Ty || !Ty->isMethodType()) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000363 cerr << "Method not meth type! Ty = " << Ty << endl;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000364 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
366
Chris Lattner1d670cc2001-09-07 16:37:43 +0000367 // When the ModuleGlobalInfo section is read, we load the type of each
368 // method and the 'ModuleValues' slot that it lands in. We then load a
369 // placeholder into its slot to reserve it. When the method is loaded, this
370 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000371
372 // Insert the placeholder...
373 Value *Def = new MethPHolder(Ty, 0);
374 insertValue(Def, ModuleValues);
375
376 // Figure out which entry of its typeslot it went into...
377 unsigned TypeSlot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000378 if (getTypeSlot(Def->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
381
382 // Keep track of this information in a linked list that is emptied as
383 // methods are loaded...
384 //
385 MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000386 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000387 BCR_TRACE(2, "Method of type: " << Ty << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000388 }
389
Chris Lattner3d3f2892001-07-28 17:50:18 +0000390 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000391
392 // This is for future proofing... in the future extra fields may be added that
393 // we don't understand, so we transparently ignore them.
394 //
395 Buf = End;
396 return false;
397}
398
399bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
400 Module *&C) {
401
402 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000403 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000404 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
Chris Lattner3d3f2892001-07-28 17:50:18 +0000405 return failure(true); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000406
Chris Lattner1d670cc2001-09-07 16:37:43 +0000407 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000408 MethodSignatureList.clear(); // Just in case...
409
410 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000411 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
412 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000413 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000414
415 C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000416 while (Buf < EndBuf) {
417 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000418 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000419 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000420 case BytecodeFormat::ConstantPool:
421 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
422 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000423 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000424 }
425 break;
426
Chris Lattner1d670cc2001-09-07 16:37:43 +0000427 case BytecodeFormat::ModuleGlobalInfo:
428 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
429
430 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000431 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000432 }
433 break;
434
435 case BytecodeFormat::Method: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000436 BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000437 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000438 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000439 }
440 break;
441 }
442
443 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000444 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
445 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000446 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000447 }
448 break;
449
450 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000451 cerr << " Unknown class block: " << Type << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000452 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000453 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000454 break;
455 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000456 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000457 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000458 }
459
460 if (!MethodSignatureList.empty()) // Expected more methods!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000461 return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000462
463 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000464 return false;
465}
466
467Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
468 LateResolveValues.clear();
469 unsigned Sig;
470 // Read and check signature...
471 if (read(Buf, EndBuf, Sig) ||
472 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000473 return failure<Module*>(0); // Invalid signature!
Chris Lattner00950542001-06-06 20:29:01 +0000474
475 Module *Result;
476 if (ParseModule(Buf, EndBuf, Result)) return 0;
477 return Result;
478}
479
480
481Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
482 BytecodeParser Parser;
483 return Parser.ParseBytecode(Buffer, Buffer+Length);
484}
485
486// Parse and return a class file...
487//
488Module *ParseBytecodeFile(const string &Filename) {
489 struct stat StatBuf;
490 Module *Result = 0;
491
492 if (Filename != string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000493 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000494 if (FD == -1) return failure<Module*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000495
Chris Lattner3d3f2892001-07-28 17:50:18 +0000496 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000497
498 int Length = StatBuf.st_size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000499 if (Length == 0) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000500 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
501 MAP_PRIVATE, FD, 0);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000502 if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000503
504 BytecodeParser Parser;
505 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
506
507 munmap((char*)Buffer, Length);
508 close(FD);
509 } else { // Read from stdin
510 size_t FileSize = 0;
511 int BlockSize;
512 uchar Buffer[4096], *FileData = 0;
513 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000514 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000515
516 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
517 memcpy(FileData+FileSize, Buffer, BlockSize);
518 FileSize += BlockSize;
519 }
520
Chris Lattner3d3f2892001-07-28 17:50:18 +0000521 if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000522
523#define ALIGN_PTRS 1
524#if ALIGN_PTRS
525 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
526 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
527 assert((Buf != (uchar*)-1) && "mmap returned error!");
528 free(FileData);
529 memcpy(Buf, FileData, FileSize);
530#else
531 uchar *Buf = FileData;
532#endif
533
534 BytecodeParser Parser;
535 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
536
537#if ALIGN_PTRS
538 munmap((char*)Buf, FileSize); // Free mmap'd data area
539#else
540 free(FileData); // Free realloc'd block of memory
541#endif
542 }
543
544 return Result;
545}