blob: 0b2e935ed4e56bf7f741fda6253e95f1d5688c74 [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Bytecode/Reader.h
4//
5// Note that this library should be as fast as possible, reentrant, and
6// threadsafe!!
7//
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//
Chris Lattnerd6b65252001-10-24 01:15:12 +000011//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000012
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Bytecode/Reader.h"
15#include "llvm/Bytecode/Format.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000016#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
18#include "llvm/BasicBlock.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000019#include "llvm/ConstantVals.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <fcntl.h>
26#include <unistd.h>
27#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000028#include <iostream>
29using std::cerr;
30using std::make_pair;
Chris Lattner00950542001-06-06 20:29:01 +000031
32bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
33 if (Ty->isPrimitiveType()) {
34 Slot = Ty->getPrimitiveID();
35 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000036 // Check the method level types first...
37 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
38 MethodTypeValues.end(), Ty);
39 if (I != MethodTypeValues.end()) {
40 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
41 (&*I - &MethodTypeValues[0]);
42 } else {
43 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
44 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
45 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
46 }
Chris Lattner00950542001-06-06 20:29:01 +000047 }
Chris Lattner697954c2002-01-20 22:54:45 +000048 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000049 return false;
50}
51
52const Type *BytecodeParser::getType(unsigned ID) {
53 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
54 if (T) return T;
55
Chris Lattner697954c2002-01-20 22:54:45 +000056 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000057
58 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +000059 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattnercfe26c92001-10-01 18:26:53 +000061 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000062}
63
Chris Lattner697954c2002-01-20 22:54:45 +000064int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000065 unsigned type;
Chris Lattner05950c32001-10-13 06:47:01 +000066 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
Chris Lattner1d670cc2001-09-07 16:37:43 +000067 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000068
69 if (ValueTab.size() <= type)
70 ValueTab.resize(type+1, ValueList());
71
72 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000073 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000074 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000075
Chris Lattner05950c32001-10-13 06:47:01 +000076 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000077}
78
79Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
80 unsigned Num = oNum;
81 unsigned type; // The type plane it lives in...
82
Chris Lattner3d3f2892001-07-28 17:50:18 +000083 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000084
85 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000086 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000087 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
88 if (T) return (Value*)T; // Asked for a primitive type...
89
90 // Otherwise, derived types need offset...
91 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000092
93 // Is it a module level type?
94 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000095 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000096
97 // Nope, is it a method level type?
98 Num -= ModuleTypeValues.size();
99 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000100 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101
102 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000103 }
104
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 if (type < ModuleValues.size()) {
106 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000107 return ModuleValues[type][Num];
108 Num -= ModuleValues[type].size();
109 }
110
111 if (Values.size() > type && Values[type].size() > Num)
112 return Values[type][Num];
113
Chris Lattner3d3f2892001-07-28 17:50:18 +0000114 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000115
116 Value *d = 0;
117 switch (Ty->getPrimitiveID()) {
118 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
119 case Type::MethodTyID:
120 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000121 << Ty->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000122 d = new MethPHolder(Ty, oNum);
Chris Lattner05950c32001-10-13 06:47:01 +0000123 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000124 return d;
125 default: d = new DefPHolder(Ty, oNum); break;
126 }
127
128 assert(d != 0 && "How did we not make something?");
Chris Lattner05950c32001-10-13 06:47:01 +0000129 if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000130 return d;
131}
132
133bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
134 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000135 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000136 ValueList &DL = ValTab[ty];
137 unsigned Size;
138 while ((Size = DL.size())) {
139 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
140
141 Value *D = DL[Size-1];
142 DL.pop_back();
143
144 Value *NewDef = getValue(D->getType(), IDNumber, false);
145 if (NewDef == 0) {
146 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000147 cerr << "Unresolvable reference found: <"
148 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000149 } else {
150 // Fixup all of the uses of this placeholder def...
151 D->replaceAllUsesWith(NewDef);
152
153 // Now that all the uses are gone, delete the placeholder...
154 // If we couldn't find a def (error case), then leak a little
155 delete D; // memory, 'cause otherwise we can't remove all uses!
156 }
157 }
158 }
159
160 return Error;
161}
162
163bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
164 BasicBlock *&BB) {
165 BB = new BasicBlock();
166
167 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000168 Instruction *Inst;
169 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000170 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000171 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000172 }
173
Chris Lattner1d670cc2001-09-07 16:37:43 +0000174 if (Inst == 0) { delete BB; return failure(true); }
Chris Lattner05950c32001-10-13 06:47:01 +0000175 if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattner1d670cc2001-09-07 16:37:43 +0000177 BB->getInstList().push_back(Inst);
178
179 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000180 }
181
182 return false;
183}
184
Chris Lattner1d670cc2001-09-07 16:37:43 +0000185bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
186 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000187 while (Buf < EndBuf) {
188 // Symtab block header: [num entries][type id number]
189 unsigned NumEntries, Typ;
190 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000191 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000192 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000193 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattner1d670cc2001-09-07 16:37:43 +0000195 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
196 " entries\n");
197
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000198 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000199 // Symtab entry: [def slot #][name]
200 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000201 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner697954c2002-01-20 22:54:45 +0000202 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000203 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000204 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000205
206 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000207 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000208 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000209 return failure(true);
210 }
211 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000212 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000213
214 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000215 }
216 }
217
Chris Lattner3d3f2892001-07-28 17:50:18 +0000218 if (Buf > EndBuf) return failure(true);
219 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000220}
221
Chris Lattner05950c32001-10-13 06:47:01 +0000222// DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000223// form of ConstantPointerRef.
Chris Lattner05950c32001-10-13 06:47:01 +0000224//
225void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
226 // Check to see if there is a forward reference to this global variable...
227 // if there is, eliminate it and patch the reference to use the new def'n.
228 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
229
230 if (I != GlobalRefs.end()) {
231 GlobalVariable *OldGV = I->second; // Get the placeholder...
232 BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
233
234 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000235 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner05950c32001-10-13 06:47:01 +0000236 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
237 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
239 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner05950c32001-10-13 06:47:01 +0000240 assert(CPPR->getValue() == OldGV && "Something isn't happy");
241
242 BCR_TRACE(4, "Mutating Forward Ref!\n");
243
244 // Change the const pool reference to point to the real global variable
245 // now. This should drop a use from the OldGV.
246 CPPR->mutateReference(GV);
247 }
248
249 // Remove GV from the module...
250 GV->getParent()->getGlobalList().remove(OldGV);
251 delete OldGV; // Delete the old placeholder
252
253 // Remove the map entry for the global now that it has been created...
254 GlobalRefs.erase(I);
255 }
256}
Chris Lattner00950542001-06-06 20:29:01 +0000257
258bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
259 Module *C) {
260 // Clear out the local values table...
261 Values.clear();
Chris Lattnerd6b65252001-10-24 01:15:12 +0000262 if (MethodSignatureList.empty()) {
263 Error = "Method found, but MethodSignatureList empty!";
264 return failure(true); // Unexpected method!
265 }
Chris Lattner00950542001-06-06 20:29:01 +0000266
Chris Lattneref9c23f2001-10-03 14:53:21 +0000267 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
Chris Lattner7a176752001-12-04 00:03:30 +0000268 const MethodType *MTy = dyn_cast<const MethodType>(PMTy->getElementType());
Chris Lattneref9c23f2001-10-03 14:53:21 +0000269 if (MTy == 0) return failure(true); // Not ptr to method!
270
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000271 unsigned isInternal;
272 if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
273
Chris Lattner00950542001-06-06 20:29:01 +0000274 unsigned MethSlot = MethodSignatureList.front().second;
275 MethodSignatureList.pop_front();
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000276 Method *M = new Method(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000277
Chris Lattner697954c2002-01-20 22:54:45 +0000278 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000279
Chris Lattner00950542001-06-06 20:29:01 +0000280 const MethodType::ParamTypes &Params = MTy->getParamTypes();
281 for (MethodType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000282 It != Params.end(); ++It) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000283 FunctionArgument *FA = new FunctionArgument(*It);
284 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000285 Error = "Error reading method arguments!\n";
286 delete M; return failure(true);
287 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000288 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000289 }
290
291 while (Buf < EndBuf) {
292 unsigned Type, Size;
293 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000294 if (readBlock(Buf, EndBuf, Type, Size)) {
295 Error = "Error reading Method level block!";
296 delete M; return failure(true);
297 }
Chris Lattner00950542001-06-06 20:29:01 +0000298
299 switch (Type) {
300 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000301 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
302 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000303 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
305 break;
306
307 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000308 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000309 BasicBlock *BB;
310 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000311 insertValue(BB, Values) == -1) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000312 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000313 }
314
315 M->getBasicBlocks().push_back(BB);
316 break;
317 }
318
319 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000320 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
321 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000322 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000323 }
324 break;
325
326 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000327 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000328 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000329 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000330 break;
331 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000332 BCR_TRACE(2, "} end block\n");
333
Chris Lattner00950542001-06-06 20:29:01 +0000334 if (align32(Buf, EndBuf)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000335 Error = "Error aligning Method level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000336 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000337 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000338 }
339 }
340
341 if (postResolveValues(LateResolveValues) ||
342 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000343 Error = "Error resolving method values!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000344 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000345 }
346
Chris Lattneref9c23f2001-10-03 14:53:21 +0000347 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000348 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000349 assert(isa<Method>(MethPHolder) && "Not a method?");
Chris Lattner00950542001-06-06 20:29:01 +0000350
351 unsigned type; // Type slot
352 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000353 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000354
Chris Lattner79df7c02002-03-26 18:01:55 +0000355 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000356
357 // Replace placeholder with the real method pointer...
358 ModuleValues[type][MethSlot] = M;
359
Chris Lattnere4d71a12001-09-14 22:03:42 +0000360 // Clear out method level types...
361 MethodTypeValues.clear();
362
Chris Lattner00950542001-06-06 20:29:01 +0000363 // If anyone is using the placeholder make them use the real method instead
364 MethPHolder->replaceAllUsesWith(M);
365
366 // We don't need the placeholder anymore!
367 delete MethPHolder;
368
Chris Lattnerb847f512001-10-14 23:28:41 +0000369 // If the method is empty, we don't need the method argument entries...
370 if (M->isExternal())
371 M->getArgumentList().delete_all();
372
Chris Lattner05950c32001-10-13 06:47:01 +0000373 DeclareNewGlobalValue(M, MethSlot);
374
Chris Lattner00950542001-06-06 20:29:01 +0000375 return false;
376}
377
378bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000379 Module *Mod) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000380 if (!MethodSignatureList.empty()) {
381 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000382 return failure(true); // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000383 }
Chris Lattner00950542001-06-06 20:29:01 +0000384
Chris Lattner70cc3392001-09-10 07:58:01 +0000385 // Read global variables...
386 unsigned VarType;
387 if (read_vbr(Buf, End, VarType)) return failure(true);
388 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000389 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
390 // bit2 = isInternal, bit3+ = slot#
391 const Type *Ty = getType(VarType >> 3);
Chris Lattner70cc3392001-09-10 07:58:01 +0000392 if (!Ty || !Ty->isPointerType()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000393 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner70cc3392001-09-10 07:58:01 +0000394 return failure(true);
395 }
396
Chris Lattneref9c23f2001-10-03 14:53:21 +0000397 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000398 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000399
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000400 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000401 if (VarType & 2) { // Does it have an initalizer?
402 // Do not improvise... values must have been stored in the constant pool,
403 // which should have been read before now.
404 //
405 unsigned InitSlot;
406 if (read_vbr(Buf, End, InitSlot)) return failure(true);
407
Chris Lattner05950c32001-10-13 06:47:01 +0000408 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000409 if (V == 0) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000410 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000411 }
412
Chris Lattner70cc3392001-09-10 07:58:01 +0000413 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000414 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
415 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000416 int DestSlot = insertValue(GV, ModuleValues);
417 if (DestSlot == -1) return failure(true);
418
419 Mod->getGlobalList().push_back(GV);
420
421 DeclareNewGlobalValue(GV, unsigned(DestSlot));
422
423 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000424 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000425
426 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattner70cc3392001-09-10 07:58:01 +0000427 }
428
Chris Lattner00950542001-06-06 20:29:01 +0000429 // Read the method signatures for all of the methods that are coming, and
430 // create fillers in the Value tables.
431 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000432 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000433 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
434 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000435 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattner7a176752001-12-04 00:03:30 +0000436 !isa<MethodType>(cast<PointerType>(Ty)->getElementType())) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000437 Error = "Method not ptr to meth type! Ty = " + Ty->getDescription();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000438 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000439 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000440
441 // We create methods by passing the underlying MethodType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000442 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000443
Chris Lattner1d670cc2001-09-07 16:37:43 +0000444 // When the ModuleGlobalInfo section is read, we load the type of each
445 // method and the 'ModuleValues' slot that it lands in. We then load a
446 // placeholder into its slot to reserve it. When the method is loaded, this
447 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000448
449 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000450 Value *Val = new MethPHolder(Ty, 0);
Chris Lattner05950c32001-10-13 06:47:01 +0000451 if (insertValue(Val, ModuleValues) == -1) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000452
453 // Figure out which entry of its typeslot it went into...
454 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000455 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000456
457 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
458
459 // Keep track of this information in a linked list that is emptied as
460 // methods are loaded...
461 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000462 MethodSignatureList.push_back(
463 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000464 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner697954c2002-01-20 22:54:45 +0000465 BCR_TRACE(2, "Method of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000466 }
467
Chris Lattner3d3f2892001-07-28 17:50:18 +0000468 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000469
470 // This is for future proofing... in the future extra fields may be added that
471 // we don't understand, so we transparently ignore them.
472 //
473 Buf = End;
474 return false;
475}
476
477bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
478 Module *&C) {
479
480 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000481 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000482 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
483 Error = "Expected Module packet!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000484 return failure(true); // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000485 }
Chris Lattner00950542001-06-06 20:29:01 +0000486
Chris Lattner1d670cc2001-09-07 16:37:43 +0000487 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000488 MethodSignatureList.clear(); // Just in case...
489
490 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000491 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
492 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000493 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000494
Chris Lattner05950c32001-10-13 06:47:01 +0000495 TheModule = C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000496 while (Buf < EndBuf) {
497 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000498 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000499 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000500 case BytecodeFormat::ConstantPool:
501 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
502 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000503 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000504 }
505 break;
506
Chris Lattner1d670cc2001-09-07 16:37:43 +0000507 case BytecodeFormat::ModuleGlobalInfo:
508 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
509
510 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000511 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000512 }
513 break;
514
515 case BytecodeFormat::Method: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000516 BCR_TRACE(1, "BLOCK BytecodeFormat::Method: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000517 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000518 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000519 }
520 break;
521 }
522
523 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000524 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
525 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000526 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000527 }
528 break;
529
530 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000531 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000532 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000533 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000534 break;
535 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000536 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000537 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000538 }
539
Chris Lattnerd6b65252001-10-24 01:15:12 +0000540 if (!MethodSignatureList.empty()) { // Expected more methods!
541 Error = "Method expected, but bytecode stream at end!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000542 return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000543 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000544
545 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000546 return false;
547}
548
549Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
550 LateResolveValues.clear();
551 unsigned Sig;
552 // Read and check signature...
553 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000554 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
555 Error = "Invalid bytecode signature!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000556 return failure<Module*>(0); // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000557 }
Chris Lattner00950542001-06-06 20:29:01 +0000558
559 Module *Result;
560 if (ParseModule(Buf, EndBuf, Result)) return 0;
561 return Result;
562}
563
564
565Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
566 BytecodeParser Parser;
567 return Parser.ParseBytecode(Buffer, Buffer+Length);
568}
569
570// Parse and return a class file...
571//
Chris Lattner697954c2002-01-20 22:54:45 +0000572Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000573 struct stat StatBuf;
574 Module *Result = 0;
575
Chris Lattner697954c2002-01-20 22:54:45 +0000576 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000577 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000578 if (FD == -1) {
579 if (ErrorStr) *ErrorStr = "Error opening file!";
580 return failure<Module*>(0);
581 }
Chris Lattner00950542001-06-06 20:29:01 +0000582
Chris Lattner3d3f2892001-07-28 17:50:18 +0000583 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000584
585 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000586 if (Length == 0) {
587 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
588 close(FD); return failure<Module*>(0);
589 }
Chris Lattner00950542001-06-06 20:29:01 +0000590 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
591 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000592 if (Buffer == (uchar*)-1) {
593 if (ErrorStr) *ErrorStr = "Error mmapping file!";
594 close(FD); return failure<Module*>(0);
595 }
Chris Lattner00950542001-06-06 20:29:01 +0000596
597 BytecodeParser Parser;
598 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
599
600 munmap((char*)Buffer, Length);
601 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000602 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000603 } else { // Read from stdin
604 size_t FileSize = 0;
605 int BlockSize;
606 uchar Buffer[4096], *FileData = 0;
607 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000608 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000609
610 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
611 memcpy(FileData+FileSize, Buffer, BlockSize);
612 FileSize += BlockSize;
613 }
614
Chris Lattnerd6b65252001-10-24 01:15:12 +0000615 if (FileSize == 0) {
616 if (ErrorStr) *ErrorStr = "Standard Input empty!";
617 free(FileData); return failure<Module*>(0);
618 }
Chris Lattner00950542001-06-06 20:29:01 +0000619
620#define ALIGN_PTRS 1
621#if ALIGN_PTRS
622 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
623 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
624 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000625 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000626 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000627#else
628 uchar *Buf = FileData;
629#endif
630
631 BytecodeParser Parser;
632 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
633
634#if ALIGN_PTRS
635 munmap((char*)Buf, FileSize); // Free mmap'd data area
636#else
637 free(FileData); // Free realloc'd block of memory
638#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000639
640 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000641 }
642
643 return Result;
644}