blob: 97428ea4ee991515c6ab57f03377186e11cb833f [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;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000209 if (!isa<Instruction>(D)) cerr << endl);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000210
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
Chris Lattneref9c23f2001-10-03 14:53:21 +0000226 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
227 const MethodType *MTy = dyn_cast<const MethodType>(PMTy->getValueType());
228 if (MTy == 0) return failure(true); // Not ptr to method!
229
Chris Lattner00950542001-06-06 20:29:01 +0000230 unsigned MethSlot = MethodSignatureList.front().second;
231 MethodSignatureList.pop_front();
232 Method *M = new Method(MTy);
233
Chris Lattner1d670cc2001-09-07 16:37:43 +0000234 BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
235
Chris Lattner00950542001-06-06 20:29:01 +0000236 const MethodType::ParamTypes &Params = MTy->getParamTypes();
237 for (MethodType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000238 It != Params.end(); ++It) {
Chris Lattner00950542001-06-06 20:29:01 +0000239 MethodArgument *MA = new MethodArgument(*It);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000240 if (insertValue(MA, Values)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000241 M->getArgumentList().push_back(MA);
242 }
243
244 while (Buf < EndBuf) {
245 unsigned Type, Size;
246 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000247 if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000248
249 switch (Type) {
250 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000251 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
252 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000253 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000254 }
255 break;
256
257 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000258 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000259 BasicBlock *BB;
260 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
261 insertValue(BB, Values)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000262 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000263 }
264
265 M->getBasicBlocks().push_back(BB);
266 break;
267 }
268
269 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000270 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
271 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000272 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000273 }
274 break;
275
276 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000277 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000278 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000279 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000280 break;
281 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000282 BCR_TRACE(2, "} end block\n");
283
Chris Lattner00950542001-06-06 20:29:01 +0000284 if (align32(Buf, EndBuf)) {
285 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000286 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000287 }
288 }
289
290 if (postResolveValues(LateResolveValues) ||
291 postResolveValues(LateResolveModuleValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000292 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000293 }
294
Chris Lattneref9c23f2001-10-03 14:53:21 +0000295 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000296 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000297 assert(isa<Method>(MethPHolder) && "Not a method?");
Chris Lattner00950542001-06-06 20:29:01 +0000298
299 unsigned type; // Type slot
300 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000301 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000302
303 C->getMethodList().push_back(M);
304
305 // Replace placeholder with the real method pointer...
306 ModuleValues[type][MethSlot] = M;
307
Chris Lattnere4d71a12001-09-14 22:03:42 +0000308 // Clear out method level types...
309 MethodTypeValues.clear();
310
Chris Lattner00950542001-06-06 20:29:01 +0000311 // If anyone is using the placeholder make them use the real method instead
312 MethPHolder->replaceAllUsesWith(M);
313
314 // We don't need the placeholder anymore!
315 delete MethPHolder;
316
317 return false;
318}
319
320bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
321 Module *C) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000322 if (!MethodSignatureList.empty())
323 return failure(true); // Two ModuleGlobal blocks?
Chris Lattner00950542001-06-06 20:29:01 +0000324
Chris Lattner70cc3392001-09-10 07:58:01 +0000325 // Read global variables...
326 unsigned VarType;
327 if (read_vbr(Buf, End, VarType)) return failure(true);
328 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd70684f2001-09-18 04:01:05 +0000329 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
330 const Type *Ty = getType(VarType >> 2);
Chris Lattner70cc3392001-09-10 07:58:01 +0000331 if (!Ty || !Ty->isPointerType()) {
332 cerr << "Global not pointer type! Ty = " << Ty << endl;
333 return failure(true);
334 }
335
Chris Lattneref9c23f2001-10-03 14:53:21 +0000336 const PointerType *PTy = cast<const PointerType>(Ty);
337 Ty = PTy->getValueType();
338
Chris Lattnerd70684f2001-09-18 04:01:05 +0000339 ConstPoolVal *Initializer = 0;
340 if (VarType & 2) { // Does it have an initalizer?
341 // Do not improvise... values must have been stored in the constant pool,
342 // which should have been read before now.
343 //
344 unsigned InitSlot;
345 if (read_vbr(Buf, End, InitSlot)) return failure(true);
346
Chris Lattneref9c23f2001-10-03 14:53:21 +0000347 Value *V = getValue(Ty, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000348 if (V == 0) return failure(true);
Chris Lattnercfe26c92001-10-01 18:26:53 +0000349 Initializer = cast<ConstPoolVal>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000350 }
351
Chris Lattner70cc3392001-09-10 07:58:01 +0000352 // Create the global variable...
Chris Lattnerd70684f2001-09-18 04:01:05 +0000353 GlobalVariable *GV = new GlobalVariable(Ty, VarType & 1, Initializer);
Chris Lattner70cc3392001-09-10 07:58:01 +0000354 insertValue(GV, ModuleValues);
355 C->getGlobalList().push_back(GV);
356
357 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000358 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription() << endl);
Chris Lattner70cc3392001-09-10 07:58:01 +0000359 }
360
Chris Lattner00950542001-06-06 20:29:01 +0000361 // Read the method signatures for all of the methods that are coming, and
362 // create fillers in the Value tables.
363 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000364 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000365 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
366 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000367 if (!Ty || !isa<PointerType>(Ty) ||
368 !isa<MethodType>(cast<PointerType>(Ty)->getValueType())) {
369 cerr << "Method not ptr to meth type! Ty = " << Ty << endl;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000370 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000371 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000372
373 // We create methods by passing the underlying MethodType to create...
374 Ty = cast<PointerType>(Ty)->getValueType();
Chris Lattner00950542001-06-06 20:29:01 +0000375
Chris Lattner1d670cc2001-09-07 16:37:43 +0000376 // When the ModuleGlobalInfo section is read, we load the type of each
377 // method and the 'ModuleValues' slot that it lands in. We then load a
378 // placeholder into its slot to reserve it. When the method is loaded, this
379 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000380
381 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000382 Value *Val = new MethPHolder(Ty, 0);
383 insertValue(Val, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +0000384
385 // Figure out which entry of its typeslot it went into...
386 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000387 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000388
389 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
390
391 // Keep track of this information in a linked list that is emptied as
392 // methods are loaded...
393 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000394 MethodSignatureList.push_back(
395 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000396 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000397 BCR_TRACE(2, "Method of type: " << Ty << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000398 }
399
Chris Lattner3d3f2892001-07-28 17:50:18 +0000400 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000401
402 // This is for future proofing... in the future extra fields may be added that
403 // we don't understand, so we transparently ignore them.
404 //
405 Buf = End;
406 return false;
407}
408
409bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
410 Module *&C) {
411
412 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000413 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000414 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
Chris Lattner3d3f2892001-07-28 17:50:18 +0000415 return failure(true); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000416
Chris Lattner1d670cc2001-09-07 16:37:43 +0000417 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000418 MethodSignatureList.clear(); // Just in case...
419
420 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000421 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
422 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000423 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000424
425 C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000426 while (Buf < EndBuf) {
427 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000428 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000429 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000430 case BytecodeFormat::ConstantPool:
431 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
432 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000433 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000434 }
435 break;
436
Chris Lattner1d670cc2001-09-07 16:37:43 +0000437 case BytecodeFormat::ModuleGlobalInfo:
438 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
439
440 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000441 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000442 }
443 break;
444
445 case BytecodeFormat::Method: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000446 BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000447 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000448 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000449 }
450 break;
451 }
452
453 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000454 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
455 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000456 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000457 }
458 break;
459
460 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000461 cerr << " Unknown class block: " << Type << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000462 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000463 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000464 break;
465 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000466 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000467 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000468 }
469
470 if (!MethodSignatureList.empty()) // Expected more methods!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000471 return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000472
473 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000474 return false;
475}
476
477Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
478 LateResolveValues.clear();
479 unsigned Sig;
480 // Read and check signature...
481 if (read(Buf, EndBuf, Sig) ||
482 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000483 return failure<Module*>(0); // Invalid signature!
Chris Lattner00950542001-06-06 20:29:01 +0000484
485 Module *Result;
486 if (ParseModule(Buf, EndBuf, Result)) return 0;
487 return Result;
488}
489
490
491Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
492 BytecodeParser Parser;
493 return Parser.ParseBytecode(Buffer, Buffer+Length);
494}
495
496// Parse and return a class file...
497//
498Module *ParseBytecodeFile(const string &Filename) {
499 struct stat StatBuf;
500 Module *Result = 0;
501
502 if (Filename != string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000503 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000504 if (FD == -1) return failure<Module*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000505
Chris Lattner3d3f2892001-07-28 17:50:18 +0000506 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000507
508 int Length = StatBuf.st_size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000509 if (Length == 0) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000510 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
511 MAP_PRIVATE, FD, 0);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000512 if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000513
514 BytecodeParser Parser;
515 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
516
517 munmap((char*)Buffer, Length);
518 close(FD);
519 } else { // Read from stdin
520 size_t FileSize = 0;
521 int BlockSize;
522 uchar Buffer[4096], *FileData = 0;
523 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000524 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000525
526 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
527 memcpy(FileData+FileSize, Buffer, BlockSize);
528 FileSize += BlockSize;
529 }
530
Chris Lattner3d3f2892001-07-28 17:50:18 +0000531 if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000532
533#define ALIGN_PTRS 1
534#if ALIGN_PTRS
535 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
536 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
537 assert((Buf != (uchar*)-1) && "mmap returned error!");
538 free(FileData);
539 memcpy(Buf, FileData, FileSize);
540#else
541 uchar *Buf = FileData;
542#endif
543
544 BytecodeParser Parser;
545 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
546
547#if ALIGN_PTRS
548 munmap((char*)Buf, FileSize); // Free mmap'd data area
549#else
550 free(FileData); // Free realloc'd block of memory
551#endif
552 }
553
554 return Result;
555}