blob: 5cb20790b91b3a9d463994f651c96476d64a0f69 [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 Lattner05950c32001-10-13 06:47:01 +000061int BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000062 unsigned type;
Chris Lattner05950c32001-10-13 06:47:01 +000063 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
Chris Lattner1d670cc2001-09-07 16:37:43 +000064 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
Chris Lattner05950c32001-10-13 06:47:01 +000073 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000074}
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())
Chris Lattner05950c32001-10-13 06:47:01 +000092 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000093
94 // Nope, is it a method level type?
95 Num -= ModuleTypeValues.size();
96 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000097 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000098
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);
Chris Lattner05950c32001-10-13 06:47:01 +0000120 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000121 return d;
122 default: d = new DefPHolder(Ty, oNum); break;
123 }
124
125 assert(d != 0 && "How did we not make something?");
Chris Lattner05950c32001-10-13 06:47:01 +0000126 if (insertValue(d, LateResolveValues) == -1) 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
Chris Lattner05950c32001-10-13 06:47:01 +0000144 cerr << "Unresolvable reference found: <"
145 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000146 } 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); }
Chris Lattner05950c32001-10-13 06:47:01 +0000172 if (insertValue(Inst, Values) == -1) { 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
Chris Lattner05950c32001-10-13 06:47:01 +0000219// DeclareNewGlobalValue - Patch up forward references to global values in the
220// form of ConstPoolPointerReferences.
221//
222void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
223 // Check to see if there is a forward reference to this global variable...
224 // if there is, eliminate it and patch the reference to use the new def'n.
225 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
226
227 if (I != GlobalRefs.end()) {
228 GlobalVariable *OldGV = I->second; // Get the placeholder...
229 BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
230
231 // Loop over all of the uses of the GlobalValue. The only thing they are
232 // allowed to be at this point is ConstPoolPointerReference's.
233 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
234 while (!OldGV->use_empty()) {
235 User *U = OldGV->use_back(); // Must be a ConstPoolPointerReference...
236 ConstPoolPointerReference *CPPR = cast<ConstPoolPointerReference>(U);
237 assert(CPPR->getValue() == OldGV && "Something isn't happy");
238
239 BCR_TRACE(4, "Mutating Forward Ref!\n");
240
241 // Change the const pool reference to point to the real global variable
242 // now. This should drop a use from the OldGV.
243 CPPR->mutateReference(GV);
244 }
245
246 // Remove GV from the module...
247 GV->getParent()->getGlobalList().remove(OldGV);
248 delete OldGV; // Delete the old placeholder
249
250 // Remove the map entry for the global now that it has been created...
251 GlobalRefs.erase(I);
252 }
253}
Chris Lattner00950542001-06-06 20:29:01 +0000254
255bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
256 Module *C) {
257 // Clear out the local values table...
258 Values.clear();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000259 if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
Chris Lattner00950542001-06-06 20:29:01 +0000260
Chris Lattneref9c23f2001-10-03 14:53:21 +0000261 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
262 const MethodType *MTy = dyn_cast<const MethodType>(PMTy->getValueType());
263 if (MTy == 0) return failure(true); // Not ptr to method!
264
Chris Lattner00950542001-06-06 20:29:01 +0000265 unsigned MethSlot = MethodSignatureList.front().second;
266 MethodSignatureList.pop_front();
267 Method *M = new Method(MTy);
268
Chris Lattner1d670cc2001-09-07 16:37:43 +0000269 BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
270
Chris Lattner00950542001-06-06 20:29:01 +0000271 const MethodType::ParamTypes &Params = MTy->getParamTypes();
272 for (MethodType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000273 It != Params.end(); ++It) {
Chris Lattner00950542001-06-06 20:29:01 +0000274 MethodArgument *MA = new MethodArgument(*It);
Chris Lattner05950c32001-10-13 06:47:01 +0000275 if (insertValue(MA, Values) == -1) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000276 M->getArgumentList().push_back(MA);
277 }
278
279 while (Buf < EndBuf) {
280 unsigned Type, Size;
281 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000282 if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000283
284 switch (Type) {
285 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000286 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
287 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000288 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000289 }
290 break;
291
292 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000293 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000294 BasicBlock *BB;
295 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000296 insertValue(BB, Values) == -1) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000297 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
299
300 M->getBasicBlocks().push_back(BB);
301 break;
302 }
303
304 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000305 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
306 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000307 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000308 }
309 break;
310
311 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000312 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000313 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000314 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000315 break;
316 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000317 BCR_TRACE(2, "} end block\n");
318
Chris Lattner00950542001-06-06 20:29:01 +0000319 if (align32(Buf, EndBuf)) {
320 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000321 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000322 }
323 }
324
325 if (postResolveValues(LateResolveValues) ||
326 postResolveValues(LateResolveModuleValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000327 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000328 }
329
Chris Lattneref9c23f2001-10-03 14:53:21 +0000330 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000331 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000332 assert(isa<Method>(MethPHolder) && "Not a method?");
Chris Lattner00950542001-06-06 20:29:01 +0000333
334 unsigned type; // Type slot
335 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000336 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000337
338 C->getMethodList().push_back(M);
339
340 // Replace placeholder with the real method pointer...
341 ModuleValues[type][MethSlot] = M;
342
Chris Lattnere4d71a12001-09-14 22:03:42 +0000343 // Clear out method level types...
344 MethodTypeValues.clear();
345
Chris Lattner00950542001-06-06 20:29:01 +0000346 // If anyone is using the placeholder make them use the real method instead
347 MethPHolder->replaceAllUsesWith(M);
348
349 // We don't need the placeholder anymore!
350 delete MethPHolder;
351
Chris Lattnerb847f512001-10-14 23:28:41 +0000352 // If the method is empty, we don't need the method argument entries...
353 if (M->isExternal())
354 M->getArgumentList().delete_all();
355
Chris Lattner05950c32001-10-13 06:47:01 +0000356 DeclareNewGlobalValue(M, MethSlot);
357
Chris Lattner00950542001-06-06 20:29:01 +0000358 return false;
359}
360
361bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000362 Module *Mod) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000363 if (!MethodSignatureList.empty())
364 return failure(true); // Two ModuleGlobal blocks?
Chris Lattner00950542001-06-06 20:29:01 +0000365
Chris Lattner70cc3392001-09-10 07:58:01 +0000366 // Read global variables...
367 unsigned VarType;
368 if (read_vbr(Buf, End, VarType)) return failure(true);
369 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd70684f2001-09-18 04:01:05 +0000370 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
371 const Type *Ty = getType(VarType >> 2);
Chris Lattner70cc3392001-09-10 07:58:01 +0000372 if (!Ty || !Ty->isPointerType()) {
373 cerr << "Global not pointer type! Ty = " << Ty << endl;
374 return failure(true);
375 }
376
Chris Lattneref9c23f2001-10-03 14:53:21 +0000377 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner05950c32001-10-13 06:47:01 +0000378 const Type *ElTy = PTy->getValueType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000379
Chris Lattnerd70684f2001-09-18 04:01:05 +0000380 ConstPoolVal *Initializer = 0;
381 if (VarType & 2) { // Does it have an initalizer?
382 // Do not improvise... values must have been stored in the constant pool,
383 // which should have been read before now.
384 //
385 unsigned InitSlot;
386 if (read_vbr(Buf, End, InitSlot)) return failure(true);
387
Chris Lattner05950c32001-10-13 06:47:01 +0000388 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000389 if (V == 0) return failure(true);
Chris Lattnercfe26c92001-10-01 18:26:53 +0000390 Initializer = cast<ConstPoolVal>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000391 }
392
Chris Lattner70cc3392001-09-10 07:58:01 +0000393 // Create the global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000394 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Initializer);
395 int DestSlot = insertValue(GV, ModuleValues);
396 if (DestSlot == -1) return failure(true);
397
398 Mod->getGlobalList().push_back(GV);
399
400 DeclareNewGlobalValue(GV, unsigned(DestSlot));
401
402 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
403 << " into slot #" << DestSlot << endl);
Chris Lattner70cc3392001-09-10 07:58:01 +0000404
405 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattner70cc3392001-09-10 07:58:01 +0000406 }
407
Chris Lattner00950542001-06-06 20:29:01 +0000408 // Read the method signatures for all of the methods that are coming, and
409 // create fillers in the Value tables.
410 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000411 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000412 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
413 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000414 if (!Ty || !isa<PointerType>(Ty) ||
415 !isa<MethodType>(cast<PointerType>(Ty)->getValueType())) {
416 cerr << "Method not ptr to meth type! Ty = " << Ty << endl;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000417 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000418 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000419
420 // We create methods by passing the underlying MethodType to create...
421 Ty = cast<PointerType>(Ty)->getValueType();
Chris Lattner00950542001-06-06 20:29:01 +0000422
Chris Lattner1d670cc2001-09-07 16:37:43 +0000423 // When the ModuleGlobalInfo section is read, we load the type of each
424 // method and the 'ModuleValues' slot that it lands in. We then load a
425 // placeholder into its slot to reserve it. When the method is loaded, this
426 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000427
428 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000429 Value *Val = new MethPHolder(Ty, 0);
Chris Lattner05950c32001-10-13 06:47:01 +0000430 if (insertValue(Val, ModuleValues) == -1) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000431
432 // Figure out which entry of its typeslot it went into...
433 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000434 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000435
436 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
437
438 // Keep track of this information in a linked list that is emptied as
439 // methods are loaded...
440 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000441 MethodSignatureList.push_back(
442 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000443 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000444 BCR_TRACE(2, "Method of type: " << Ty << endl);
Chris Lattner00950542001-06-06 20:29:01 +0000445 }
446
Chris Lattner3d3f2892001-07-28 17:50:18 +0000447 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000448
449 // This is for future proofing... in the future extra fields may be added that
450 // we don't understand, so we transparently ignore them.
451 //
452 Buf = End;
453 return false;
454}
455
456bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
457 Module *&C) {
458
459 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000460 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000461 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
Chris Lattner3d3f2892001-07-28 17:50:18 +0000462 return failure(true); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000463
Chris Lattner1d670cc2001-09-07 16:37:43 +0000464 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000465 MethodSignatureList.clear(); // Just in case...
466
467 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000468 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
469 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000470 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000471
Chris Lattner05950c32001-10-13 06:47:01 +0000472 TheModule = C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000473 while (Buf < EndBuf) {
474 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000475 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000476 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000477 case BytecodeFormat::ConstantPool:
478 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
479 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000480 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000481 }
482 break;
483
Chris Lattner1d670cc2001-09-07 16:37:43 +0000484 case BytecodeFormat::ModuleGlobalInfo:
485 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
486
487 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000488 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000489 }
490 break;
491
492 case BytecodeFormat::Method: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000493 BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000494 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000495 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000496 }
497 break;
498 }
499
500 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000501 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
502 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000503 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000504 }
505 break;
506
507 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000508 cerr << " Unknown class block: " << Type << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000509 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000510 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000511 break;
512 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000513 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000514 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000515 }
516
517 if (!MethodSignatureList.empty()) // Expected more methods!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000518 return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000519
520 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000521 return false;
522}
523
524Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
525 LateResolveValues.clear();
526 unsigned Sig;
527 // Read and check signature...
528 if (read(Buf, EndBuf, Sig) ||
529 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000530 return failure<Module*>(0); // Invalid signature!
Chris Lattner00950542001-06-06 20:29:01 +0000531
532 Module *Result;
533 if (ParseModule(Buf, EndBuf, Result)) return 0;
534 return Result;
535}
536
537
538Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
539 BytecodeParser Parser;
540 return Parser.ParseBytecode(Buffer, Buffer+Length);
541}
542
543// Parse and return a class file...
544//
545Module *ParseBytecodeFile(const string &Filename) {
546 struct stat StatBuf;
547 Module *Result = 0;
548
549 if (Filename != string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000550 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000551 if (FD == -1) return failure<Module*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000552
Chris Lattner3d3f2892001-07-28 17:50:18 +0000553 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000554
555 int Length = StatBuf.st_size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000556 if (Length == 0) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000557 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
558 MAP_PRIVATE, FD, 0);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000559 if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000560
561 BytecodeParser Parser;
562 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
563
564 munmap((char*)Buffer, Length);
565 close(FD);
566 } else { // Read from stdin
567 size_t FileSize = 0;
568 int BlockSize;
569 uchar Buffer[4096], *FileData = 0;
570 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000571 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000572
573 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
574 memcpy(FileData+FileSize, Buffer, BlockSize);
575 FileSize += BlockSize;
576 }
577
Chris Lattner3d3f2892001-07-28 17:50:18 +0000578 if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000579
580#define ALIGN_PTRS 1
581#if ALIGN_PTRS
582 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
583 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
584 assert((Buf != (uchar*)-1) && "mmap returned error!");
585 free(FileData);
586 memcpy(Buf, FileData, FileSize);
587#else
588 uchar *Buf = FileData;
589#endif
590
591 BytecodeParser Parser;
592 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
593
594#if ALIGN_PTRS
595 munmap((char*)Buf, FileSize); // Free mmap'd data area
596#else
597 free(FileData); // Free realloc'd block of memory
598#endif
599 }
600
601 return Result;
602}