blob: c03191c9d9c66ba8e2b44179f921910dcc9661b9 [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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000022#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <fcntl.h>
25#include <unistd.h>
26#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000027#include <iostream>
28using std::cerr;
29using std::make_pair;
Chris Lattner00950542001-06-06 20:29:01 +000030
31bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
32 if (Ty->isPrimitiveType()) {
33 Slot = Ty->getPrimitiveID();
34 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000035 // Check the method level types first...
36 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
37 MethodTypeValues.end(), Ty);
38 if (I != MethodTypeValues.end()) {
39 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
40 (&*I - &MethodTypeValues[0]);
41 } else {
42 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
43 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
44 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
45 }
Chris Lattner00950542001-06-06 20:29:01 +000046 }
Chris Lattner697954c2002-01-20 22:54:45 +000047 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000048 return false;
49}
50
51const Type *BytecodeParser::getType(unsigned ID) {
52 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
53 if (T) return T;
54
Chris Lattner697954c2002-01-20 22:54:45 +000055 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000056
57 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +000058 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattnercfe26c92001-10-01 18:26:53 +000060 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000061}
62
Chris Lattner697954c2002-01-20 22:54:45 +000063int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000064 unsigned type;
Chris Lattner05950c32001-10-13 06:47:01 +000065 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
Chris Lattner1d670cc2001-09-07 16:37:43 +000066 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000067
68 if (ValueTab.size() <= type)
69 ValueTab.resize(type+1, ValueList());
70
71 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000072 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000073 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattner05950c32001-10-13 06:47:01 +000075 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000076}
77
78Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
79 unsigned Num = oNum;
80 unsigned type; // The type plane it lives in...
81
Chris Lattner3d3f2892001-07-28 17:50:18 +000082 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000083
84 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000086 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
87 if (T) return (Value*)T; // Asked for a primitive type...
88
89 // Otherwise, derived types need offset...
90 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000091
92 // Is it a module level type?
93 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000094 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000095
96 // Nope, is it a method level type?
97 Num -= ModuleTypeValues.size();
98 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000099 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000100
101 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104 if (type < ModuleValues.size()) {
105 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000106 return ModuleValues[type][Num];
107 Num -= ModuleValues[type].size();
108 }
109
110 if (Values.size() > type && Values[type].size() > Num)
111 return Values[type][Num];
112
Chris Lattner3d3f2892001-07-28 17:50:18 +0000113 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000114
115 Value *d = 0;
116 switch (Ty->getPrimitiveID()) {
117 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000118 case Type::FunctionTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000119 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000120 << Ty->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000121 d = new MethPHolder(Ty, oNum);
Chris Lattner05950c32001-10-13 06:47:01 +0000122 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000123 return d;
124 default: d = new DefPHolder(Ty, oNum); break;
125 }
126
127 assert(d != 0 && "How did we not make something?");
Chris Lattner05950c32001-10-13 06:47:01 +0000128 if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000129 return d;
130}
131
132bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
133 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000134 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000135 ValueList &DL = ValTab[ty];
136 unsigned Size;
137 while ((Size = DL.size())) {
138 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
139
140 Value *D = DL[Size-1];
141 DL.pop_back();
142
143 Value *NewDef = getValue(D->getType(), IDNumber, false);
144 if (NewDef == 0) {
145 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000146 cerr << "Unresolvable reference found: <"
147 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000148 } else {
149 // Fixup all of the uses of this placeholder def...
150 D->replaceAllUsesWith(NewDef);
151
152 // Now that all the uses are gone, delete the placeholder...
153 // If we couldn't find a def (error case), then leak a little
154 delete D; // memory, 'cause otherwise we can't remove all uses!
155 }
156 }
157 }
158
159 return Error;
160}
161
162bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
163 BasicBlock *&BB) {
164 BB = new BasicBlock();
165
166 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000167 Instruction *Inst;
168 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000169 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000170 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000171 }
172
Chris Lattner1d670cc2001-09-07 16:37:43 +0000173 if (Inst == 0) { delete BB; return failure(true); }
Chris Lattner05950c32001-10-13 06:47:01 +0000174 if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000175
Chris Lattner1d670cc2001-09-07 16:37:43 +0000176 BB->getInstList().push_back(Inst);
177
178 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000179 }
180
181 return false;
182}
183
Chris Lattner1d670cc2001-09-07 16:37:43 +0000184bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
185 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 while (Buf < EndBuf) {
187 // Symtab block header: [num entries][type id number]
188 unsigned NumEntries, Typ;
189 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000190 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000191 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000192 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000193
Chris Lattner1d670cc2001-09-07 16:37:43 +0000194 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
195 " entries\n");
196
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000197 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 // Symtab entry: [def slot #][name]
199 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000200 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner697954c2002-01-20 22:54:45 +0000201 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000202 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000203 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000204
205 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000206 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000207 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000208 return failure(true);
209 }
210 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000211 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000212
213 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000214 }
215 }
216
Chris Lattner3d3f2892001-07-28 17:50:18 +0000217 if (Buf > EndBuf) return failure(true);
218 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000219}
220
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000221Value*
222ConstantFwdRefs::find(const Type* Ty, unsigned Slot) {
223 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot));
Chris Lattner05950c32001-10-13 06:47:01 +0000224 if (I != GlobalRefs.end()) {
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000225 return I->second;
226 } else {
227 return failure<Value*>(0);
Chris Lattner05950c32001-10-13 06:47:01 +0000228 }
229}
Chris Lattner00950542001-06-06 20:29:01 +0000230
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000231void
232ConstantFwdRefs::insert(const Type* Ty, unsigned Slot, Value* V) {
233 // Keep track of the fact that we have a forward ref to recycle it
234 const pair<GlobalRefsType::iterator, bool>& result =
235 GlobalRefs.insert(make_pair(make_pair(Ty, Slot), V));
236 assert(result.second == true && "Entry already exists for this slot?");
237}
238
239void
240ConstantFwdRefs::erase(const Type* Ty, unsigned Slot) {
241 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot));
242 if (I != GlobalRefs.end())
243 GlobalRefs.erase(I);
244}
245
246// GetFwdRefToConstant - Get a forward reference to a constant value.
247// Create a unique one if it does not exist already.
248//
249Constant*
250ConstantFwdRefs::GetFwdRefToConstant(const Type* Ty, unsigned Slot) {
251
252 Constant* C = cast_or_null<Constant>(find(Ty, Slot));
253
254 if (C) {
255 BCR_TRACE(5, "Previous forward ref found!\n");
256 } else {
257 // Create a placeholder for the constant reference and
258 // keep track of the fact that we have a forward ref to recycle it
259 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
260 C = new ConstPHolder(Ty, Slot);
261 insert(Ty, Slot, C);
262 }
263
264 return C;
265}
266
267
268// GetFwdRefToGlobal - Get a forward reference to a global value.
269// Create a unique one if it does not exist already.
270//
271GlobalValue*
272ConstantFwdRefs::GetFwdRefToGlobal(const PointerType* PT, unsigned Slot) {
273
274 GlobalValue* GV = cast_or_null<GlobalValue>(find(PT, Slot));
275
276 if (GV) {
277 BCR_TRACE(5, "Previous forward ref found!\n");
278 } else {
279 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
280
281 // Create a placeholder for the global variable reference...
282 GlobalVariable *GVar =
283 new GlobalVariable(PT->getElementType(), false, true);
284
285 // Keep track of the fact that we have a forward ref to recycle it
286 insert(PT, Slot, GVar);
287
288 // Must temporarily push this value into the module table...
289 TheModule->getGlobalList().push_back(GVar);
290 GV = GVar;
291 }
292
293 return GV;
294}
295
296void
297ConstantFwdRefs::ResolveRefsToValue(Value* NewV, unsigned Slot) {
298 if (Value* vph = find(NewV->getType(), Slot)) {
299 BCR_TRACE(3, "Mutating forward refs!\n");
300
301 // Loop over all of the uses of the Value. What they are depends
302 // on what NewV is. Replacing a use of the old reference takes the
303 // use off the use list, so loop with !use_empty(), not the use_iterator.
304 while (!vph->use_empty()) {
305 Constant *C = cast<Constant>(vph->use_back());
306 unsigned numReplaced = C->mutateReferences(vph, NewV);
307 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
308
309 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
310 // Remove the placeholder GlobalValue from the module...
311 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(vph));
312 }
313 }
314
315 delete vph; // Delete the old placeholder
316 erase(NewV->getType(), Slot); // Remove the map entry for it
317 }
318}
319
320// resolveRefsToGlobal - Patch up forward references to global values in the
321// form of ConstantPointerRef.
322//
323void BytecodeParser::resolveRefsToGlobal(GlobalValue *GV, unsigned Slot) {
324 fwdRefs.ResolveRefsToValue(GV, Slot);
325}
326
327// resolveRefsToConstant - Patch up forward references to constants
328//
329void BytecodeParser::resolveRefsToConstant(Constant *C, unsigned Slot) {
330 fwdRefs.ResolveRefsToValue(C, Slot);
331}
332
333
Chris Lattner00950542001-06-06 20:29:01 +0000334bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
335 Module *C) {
336 // Clear out the local values table...
337 Values.clear();
Chris Lattnerd6b65252001-10-24 01:15:12 +0000338 if (MethodSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000339 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000340 return failure(true); // Unexpected method!
341 }
Chris Lattner00950542001-06-06 20:29:01 +0000342
Chris Lattneref9c23f2001-10-03 14:53:21 +0000343 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000344 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattneref9c23f2001-10-03 14:53:21 +0000345 if (MTy == 0) return failure(true); // Not ptr to method!
346
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000347 unsigned isInternal;
348 if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
349
Chris Lattner00950542001-06-06 20:29:01 +0000350 unsigned MethSlot = MethodSignatureList.front().second;
351 MethodSignatureList.pop_front();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000352 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000353
Chris Lattner697954c2002-01-20 22:54:45 +0000354 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000355
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000356 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
357 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000358 It != Params.end(); ++It) {
Chris Lattner73e21422002-04-09 19:48:49 +0000359 Argument *FA = new Argument(*It);
Chris Lattner79df7c02002-03-26 18:01:55 +0000360 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000361 Error = "Error reading method arguments!\n";
362 delete M; return failure(true);
363 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000364 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
366
367 while (Buf < EndBuf) {
368 unsigned Type, Size;
369 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000370 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000371 Error = "Error reading Function level block!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000372 delete M; return failure(true);
373 }
Chris Lattner00950542001-06-06 20:29:01 +0000374
375 switch (Type) {
376 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000377 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
378 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000379 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000380 }
381 break;
382
383 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000384 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000385 BasicBlock *BB;
386 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000387 insertValue(BB, Values) == -1) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000388 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000389 }
390
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000391 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000392 break;
393 }
394
395 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000396 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
397 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000398 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000399 }
400 break;
401
402 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000403 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000404 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000405 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000406 break;
407 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000408 BCR_TRACE(2, "} end block\n");
409
Chris Lattner00950542001-06-06 20:29:01 +0000410 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000411 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000412 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000413 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000414 }
415 }
416
417 if (postResolveValues(LateResolveValues) ||
418 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000419 Error = "Error resolving method values!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000420 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000421 }
422
Chris Lattneref9c23f2001-10-03 14:53:21 +0000423 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000424 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000425 assert(isa<Function>(MethPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000426
427 unsigned type; // Type slot
428 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000429 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000430
Chris Lattner79df7c02002-03-26 18:01:55 +0000431 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000432
433 // Replace placeholder with the real method pointer...
434 ModuleValues[type][MethSlot] = M;
435
Chris Lattnere4d71a12001-09-14 22:03:42 +0000436 // Clear out method level types...
437 MethodTypeValues.clear();
438
Chris Lattner00950542001-06-06 20:29:01 +0000439 // If anyone is using the placeholder make them use the real method instead
440 MethPHolder->replaceAllUsesWith(M);
441
442 // We don't need the placeholder anymore!
443 delete MethPHolder;
444
Chris Lattnerb847f512001-10-14 23:28:41 +0000445 // If the method is empty, we don't need the method argument entries...
446 if (M->isExternal())
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000447 M->getArgumentList().clear();
Chris Lattnerb847f512001-10-14 23:28:41 +0000448
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000449 resolveRefsToGlobal(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000450
Chris Lattner00950542001-06-06 20:29:01 +0000451 return false;
452}
453
454bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000455 Module *Mod) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000456 if (!MethodSignatureList.empty()) {
457 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000458 return failure(true); // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000459 }
Chris Lattner00950542001-06-06 20:29:01 +0000460
Chris Lattner70cc3392001-09-10 07:58:01 +0000461 // Read global variables...
462 unsigned VarType;
463 if (read_vbr(Buf, End, VarType)) return failure(true);
464 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000465 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
466 // bit2 = isInternal, bit3+ = slot#
467 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000468 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000469 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner70cc3392001-09-10 07:58:01 +0000470 return failure(true);
471 }
472
Chris Lattneref9c23f2001-10-03 14:53:21 +0000473 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000474 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000475
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000476 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000477 if (VarType & 2) { // Does it have an initalizer?
478 // Do not improvise... values must have been stored in the constant pool,
479 // which should have been read before now.
480 //
481 unsigned InitSlot;
482 if (read_vbr(Buf, End, InitSlot)) return failure(true);
483
Chris Lattner05950c32001-10-13 06:47:01 +0000484 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000485 if (V == 0) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000486 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000487 }
488
Chris Lattner70cc3392001-09-10 07:58:01 +0000489 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000490 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
491 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000492 int DestSlot = insertValue(GV, ModuleValues);
493 if (DestSlot == -1) return failure(true);
494
495 Mod->getGlobalList().push_back(GV);
496
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000497 resolveRefsToGlobal(GV, unsigned(DestSlot));
Chris Lattner05950c32001-10-13 06:47:01 +0000498
499 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000500 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000501
502 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattner70cc3392001-09-10 07:58:01 +0000503 }
504
Chris Lattner00950542001-06-06 20:29:01 +0000505 // Read the method signatures for all of the methods that are coming, and
506 // create fillers in the Value tables.
507 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000508 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000509 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
510 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000511 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000512 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
513 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000514 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000515 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000516
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000517 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000518 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000519
Chris Lattner1d670cc2001-09-07 16:37:43 +0000520 // When the ModuleGlobalInfo section is read, we load the type of each
521 // method and the 'ModuleValues' slot that it lands in. We then load a
522 // placeholder into its slot to reserve it. When the method is loaded, this
523 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000524
525 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000526 Value *Val = new MethPHolder(Ty, 0);
Chris Lattner05950c32001-10-13 06:47:01 +0000527 if (insertValue(Val, ModuleValues) == -1) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000528
529 // Figure out which entry of its typeslot it went into...
530 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000531 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000532
533 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
534
535 // Keep track of this information in a linked list that is emptied as
536 // methods are loaded...
537 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000538 MethodSignatureList.push_back(
539 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000540 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000541 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000542 }
543
Chris Lattner3d3f2892001-07-28 17:50:18 +0000544 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000545
546 // This is for future proofing... in the future extra fields may be added that
547 // we don't understand, so we transparently ignore them.
548 //
549 Buf = End;
550 return false;
551}
552
553bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
554 Module *&C) {
555
556 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000557 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000558 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
559 Error = "Expected Module packet!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000560 return failure(true); // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000561 }
Chris Lattner00950542001-06-06 20:29:01 +0000562
Chris Lattner1d670cc2001-09-07 16:37:43 +0000563 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000564 MethodSignatureList.clear(); // Just in case...
565
566 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000567 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
568 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000569 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000570
Chris Lattner05950c32001-10-13 06:47:01 +0000571 TheModule = C = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000572 fwdRefs.VisitingModule(TheModule);
573
Chris Lattner00950542001-06-06 20:29:01 +0000574 while (Buf < EndBuf) {
575 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000576 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000577 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000578 case BytecodeFormat::ConstantPool:
579 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
580 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000581 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000582 }
583 break;
584
Chris Lattner1d670cc2001-09-07 16:37:43 +0000585 case BytecodeFormat::ModuleGlobalInfo:
586 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
587
588 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000589 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000590 }
591 break;
592
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000593 case BytecodeFormat::Function: {
594 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000595 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000596 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000597 }
598 break;
599 }
600
601 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000602 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
603 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000604 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000605 }
606 break;
607
608 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000609 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000610 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000611 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000612 break;
613 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000614 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000615 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000616 }
617
Chris Lattnerd6b65252001-10-24 01:15:12 +0000618 if (!MethodSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000619 Error = "Function expected, but bytecode stream at end!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000620 return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000621 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000622
623 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000624 return false;
625}
626
627Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
628 LateResolveValues.clear();
629 unsigned Sig;
630 // Read and check signature...
631 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000632 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
633 Error = "Invalid bytecode signature!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000634 return failure<Module*>(0); // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000635 }
Chris Lattner00950542001-06-06 20:29:01 +0000636
637 Module *Result;
638 if (ParseModule(Buf, EndBuf, Result)) return 0;
639 return Result;
640}
641
642
643Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
644 BytecodeParser Parser;
645 return Parser.ParseBytecode(Buffer, Buffer+Length);
646}
647
648// Parse and return a class file...
649//
Chris Lattner697954c2002-01-20 22:54:45 +0000650Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000651 struct stat StatBuf;
652 Module *Result = 0;
653
Chris Lattner697954c2002-01-20 22:54:45 +0000654 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000655 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000656 if (FD == -1) {
657 if (ErrorStr) *ErrorStr = "Error opening file!";
658 return failure<Module*>(0);
659 }
Chris Lattner00950542001-06-06 20:29:01 +0000660
Chris Lattner3d3f2892001-07-28 17:50:18 +0000661 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000662
663 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000664 if (Length == 0) {
665 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
666 close(FD); return failure<Module*>(0);
667 }
Chris Lattner00950542001-06-06 20:29:01 +0000668 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
669 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000670 if (Buffer == (uchar*)-1) {
671 if (ErrorStr) *ErrorStr = "Error mmapping file!";
672 close(FD); return failure<Module*>(0);
673 }
Chris Lattner00950542001-06-06 20:29:01 +0000674
675 BytecodeParser Parser;
676 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
677
678 munmap((char*)Buffer, Length);
679 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000680 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000681 } else { // Read from stdin
682 size_t FileSize = 0;
683 int BlockSize;
684 uchar Buffer[4096], *FileData = 0;
685 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000686 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000687
688 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
689 memcpy(FileData+FileSize, Buffer, BlockSize);
690 FileSize += BlockSize;
691 }
692
Chris Lattnerd6b65252001-10-24 01:15:12 +0000693 if (FileSize == 0) {
694 if (ErrorStr) *ErrorStr = "Standard Input empty!";
695 free(FileData); return failure<Module*>(0);
696 }
Chris Lattner00950542001-06-06 20:29:01 +0000697
698#define ALIGN_PTRS 1
699#if ALIGN_PTRS
700 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
701 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
702 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000703 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000704 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000705#else
706 uchar *Buf = FileData;
707#endif
708
709 BytecodeParser Parser;
710 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
711
712#if ALIGN_PTRS
713 munmap((char*)Buf, FileSize); // Free mmap'd data area
714#else
715 free(FileData); // Free realloc'd block of memory
716#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000717
718 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000719 }
720
721 return Result;
722}