blob: 0f9889c4c8eff14a46b7146a02f608260a9a3711 [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//
Chris Lattner74734132002-08-17 22:01:27 +00008// TODO: Return error messages to caller instead of printing them out directly.
Chris Lattner00950542001-06-06 20:29:01 +00009// 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"
16#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000021#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <fcntl.h>
24#include <unistd.h>
25#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Anand Shukla3edfb642002-07-16 00:04:57 +000027using std::pair;
Chris Lattner697954c2002-01-20 22:54:45 +000028using std::make_pair;
Chris Lattner00950542001-06-06 20:29:01 +000029
30bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
31 if (Ty->isPrimitiveType()) {
32 Slot = Ty->getPrimitiveID();
33 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000034 // Check the method level types first...
35 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
36 MethodTypeValues.end(), Ty);
37 if (I != MethodTypeValues.end()) {
38 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
39 (&*I - &MethodTypeValues[0]);
40 } else {
41 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
42 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
43 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
44 }
Chris Lattner00950542001-06-06 20:29:01 +000045 }
Chris Lattner697954c2002-01-20 22:54:45 +000046 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000047 return false;
48}
49
50const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000051 if (ID < Type::NumPrimitiveIDs) {
52 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
53 if (T) return T;
54 }
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner697954c2002-01-20 22:54:45 +000056 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000057 const Value *V = getValue(Type::TypeTy, ID, false);
58 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000059}
60
Chris Lattner697954c2002-01-20 22:54:45 +000061int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000062 unsigned type;
Chris Lattner74734132002-08-17 22:01:27 +000063 if (getTypeSlot(Val->getType(), type)) return -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 Lattner697954c2002-01-20 22:54:45 +000070 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000071 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 Lattner74734132002-08-17 22:01:27 +000080 if (getTypeSlot(Ty, type)) return 0;
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 Lattner8cdc6b72002-10-23 00:51:54 +000084 if (Num < Type::NumPrimitiveIDs) {
85 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
86 if (T) return (Value*)T; // Asked for a primitive type...
87 }
Chris Lattner00950542001-06-06 20:29:01 +000088
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 Lattner74734132002-08-17 22:01:27 +0000113 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000114
115 Value *d = 0;
116 switch (Ty->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000117 case Type::FunctionTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000118 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000119 << Ty->getName() << "\n";
Chris Lattner74734132002-08-17 22:01:27 +0000120 d = new FunctionPHolder(Ty, oNum);
121 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000122 return d;
Chris Lattner74734132002-08-17 22:01:27 +0000123 case Type::LabelTyID:
124 d = new BBPHolder(Ty, oNum);
125 break;
126 default:
127 d = new ValPHolder(Ty, oNum);
128 break;
Chris Lattner00950542001-06-06 20:29:01 +0000129 }
130
131 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000132 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000133 return d;
134}
135
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000136/// getConstantValue - Just like getValue, except that it returns a null pointer
137/// only on error. It always returns a constant (meaning that if the value is
138/// defined, but is not a constant, that is an error). If the specified
139/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
140/// after the real value is parsed, the placeholder is eliminated.
141///
142Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
143 if (Value *V = getValue(Ty, Slot, false))
144 return dyn_cast<Constant>(V); // If we already have the value parsed...
145
146 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot));
147 if (I != GlobalRefs.end()) {
148 BCR_TRACE(5, "Previous forward ref found!\n");
149 return cast<Constant>(I->second);
150 } else {
151 // Create a placeholder for the constant reference and
152 // keep track of the fact that we have a forward ref to recycle it
153 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
154 Constant *C = new ConstPHolder(Ty, Slot);
155
156 // Keep track of the fact that we have a forward ref to recycle it
157 GlobalRefs.insert(make_pair(make_pair(Ty, Slot), C));
158 return C;
159 }
160}
161
162
163
Chris Lattner00950542001-06-06 20:29:01 +0000164bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
165 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000166 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000167 ValueList &DL = ValTab[ty];
168 unsigned Size;
169 while ((Size = DL.size())) {
170 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
171
172 Value *D = DL[Size-1];
173 DL.pop_back();
174
175 Value *NewDef = getValue(D->getType(), IDNumber, false);
176 if (NewDef == 0) {
177 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000178 cerr << "Unresolvable reference found: <"
179 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000180 } else {
181 // Fixup all of the uses of this placeholder def...
182 D->replaceAllUsesWith(NewDef);
183
184 // Now that all the uses are gone, delete the placeholder...
185 // If we couldn't find a def (error case), then leak a little
186 delete D; // memory, 'cause otherwise we can't remove all uses!
187 }
188 }
189 }
190
191 return Error;
192}
193
194bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
195 BasicBlock *&BB) {
196 BB = new BasicBlock();
197
198 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000199 Instruction *Inst;
Chris Lattner352eef72002-08-21 22:55:27 +0000200 if (ParseInstruction(Buf, EndBuf, Inst,
201 /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000202 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000203 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000204 }
205
Chris Lattner74734132002-08-17 22:01:27 +0000206 if (Inst == 0) { delete BB; return true; }
207 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner1d670cc2001-09-07 16:37:43 +0000209 BB->getInstList().push_back(Inst);
210
211 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000212 }
213
214 return false;
215}
216
Chris Lattner1d670cc2001-09-07 16:37:43 +0000217bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
218 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000219 while (Buf < EndBuf) {
220 // Symtab block header: [num entries][type id number]
221 unsigned NumEntries, Typ;
222 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000223 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000224 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000225 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000226
Chris Lattner1d670cc2001-09-07 16:37:43 +0000227 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
228 " entries\n");
229
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000230 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000231 // Symtab entry: [def slot #][name]
232 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000233 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000234 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000235 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000236 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000237
238 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000239 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000240 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000241 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000242 }
243 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000244 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000245
246 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000247 }
248 }
249
Chris Lattner74734132002-08-17 22:01:27 +0000250 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000251 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000252}
253
Chris Lattner74734132002-08-17 22:01:27 +0000254void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
255 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(NewV->getType(),Slot));
256 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000257
Chris Lattner74734132002-08-17 22:01:27 +0000258 BCR_TRACE(3, "Mutating forward refs!\n");
259 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000260
Chris Lattner74734132002-08-17 22:01:27 +0000261 // Loop over all of the uses of the Value. What they are depends
262 // on what NewV is. Replacing a use of the old reference takes the
263 // use off the use list, so loop with !use_empty(), not the use_iterator.
264 while (!VPH->use_empty()) {
265 Constant *C = cast<Constant>(VPH->use_back());
266 unsigned numReplaced = C->mutateReferences(VPH, NewV);
267 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000268
Chris Lattner74734132002-08-17 22:01:27 +0000269 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
270 // Remove the placeholder GlobalValue from the module...
271 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000272 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000273 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000274
Chris Lattner74734132002-08-17 22:01:27 +0000275 delete VPH; // Delete the old placeholder
276 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000277}
278
Chris Lattner00950542001-06-06 20:29:01 +0000279bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
280 Module *C) {
281 // Clear out the local values table...
282 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000283 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000284 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000285 return true; // Unexpected method!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000286 }
Chris Lattner00950542001-06-06 20:29:01 +0000287
Chris Lattner74734132002-08-17 22:01:27 +0000288 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000289 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000290 if (MTy == 0) return true; // Not ptr to method!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000291
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000292 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000293 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000294
Chris Lattner74734132002-08-17 22:01:27 +0000295 unsigned MethSlot = FunctionSignatureList.back().second;
296 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000297 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000298
Chris Lattner697954c2002-01-20 22:54:45 +0000299 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000301 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000302 Function::aiterator AI = M->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000303 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000304 It != Params.end(); ++It, ++AI) {
305 if (insertValue(AI, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000306 Error = "Error reading method arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000307 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000308 }
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
310
311 while (Buf < EndBuf) {
312 unsigned Type, Size;
313 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000314 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000315 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000316 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000317 }
Chris Lattner00950542001-06-06 20:29:01 +0000318
319 switch (Type) {
320 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000321 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
322 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000323 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000324 }
325 break;
326
327 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000328 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000329 BasicBlock *BB;
330 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000331 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000332 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000333 }
334
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000335 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000336 break;
337 }
338
339 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000340 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
341 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner74734132002-08-17 22:01:27 +0000342 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000343 }
344 break;
345
346 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000347 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000348 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000349 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000350 break;
351 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000352 BCR_TRACE(2, "} end block\n");
353
Chris Lattner00950542001-06-06 20:29:01 +0000354 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000355 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000356 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000357 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000358 }
359 }
360
361 if (postResolveValues(LateResolveValues) ||
362 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000363 Error = "Error resolving method values!";
Chris Lattner74734132002-08-17 22:01:27 +0000364 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
366
Chris Lattner74734132002-08-17 22:01:27 +0000367 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
368 assert(FunctionPHolder && "Something is broken no placeholder found!");
369 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000370
371 unsigned type; // Type slot
372 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000373 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000374
Chris Lattner79df7c02002-03-26 18:01:55 +0000375 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000376
377 // Replace placeholder with the real method pointer...
378 ModuleValues[type][MethSlot] = M;
379
Chris Lattnere4d71a12001-09-14 22:03:42 +0000380 // Clear out method level types...
381 MethodTypeValues.clear();
382
Chris Lattner00950542001-06-06 20:29:01 +0000383 // If anyone is using the placeholder make them use the real method instead
Chris Lattner74734132002-08-17 22:01:27 +0000384 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000387 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000388
Chris Lattner74734132002-08-17 22:01:27 +0000389 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000390
Chris Lattner00950542001-06-06 20:29:01 +0000391 return false;
392}
393
394bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000395 Module *Mod) {
Chris Lattner74734132002-08-17 22:01:27 +0000396 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000397 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000398 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000399 }
Chris Lattner00950542001-06-06 20:29:01 +0000400
Chris Lattner70cc3392001-09-10 07:58:01 +0000401 // Read global variables...
402 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000403 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000404 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000405 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
406 // bit2 = isInternal, bit3+ = slot#
407 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000408 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000409 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000410 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000411 }
412
Chris Lattneref9c23f2001-10-03 14:53:21 +0000413 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000414 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000415
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000416 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000417 if (VarType & 2) { // Does it have an initalizer?
418 // Do not improvise... values must have been stored in the constant pool,
419 // which should have been read before now.
420 //
421 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000422 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000423
Chris Lattner05950c32001-10-13 06:47:01 +0000424 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000425 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000426 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000427 }
428
Chris Lattner70cc3392001-09-10 07:58:01 +0000429 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000430 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
431 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000432 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000433 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000434
435 Mod->getGlobalList().push_back(GV);
436
Chris Lattner74734132002-08-17 22:01:27 +0000437 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000438
439 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000440 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000441
Chris Lattner74734132002-08-17 22:01:27 +0000442 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000443 }
444
Chris Lattner00950542001-06-06 20:29:01 +0000445 // Read the method signatures for all of the methods that are coming, and
446 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000447 unsigned FnSignature;
448 if (read_vbr(Buf, End, FnSignature)) return true;
449 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
450 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000451 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000452 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
453 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000454 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000455 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000456
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000457 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000458 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000459
Chris Lattner1d670cc2001-09-07 16:37:43 +0000460 // When the ModuleGlobalInfo section is read, we load the type of each
461 // method and the 'ModuleValues' slot that it lands in. We then load a
462 // placeholder into its slot to reserve it. When the method is loaded, this
463 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000464
465 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000466 Value *Val = new FunctionPHolder(Ty, 0);
467 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000468
469 // Figure out which entry of its typeslot it went into...
470 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000471 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000472
473 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
474
475 // Keep track of this information in a linked list that is emptied as
476 // methods are loaded...
477 //
Chris Lattner74734132002-08-17 22:01:27 +0000478 FunctionSignatureList.push_back(
Chris Lattneref9c23f2001-10-03 14:53:21 +0000479 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000480 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000481 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000482 }
483
Chris Lattner74734132002-08-17 22:01:27 +0000484 if (align32(Buf, End)) return true;
485
486 // Now that the function signature list is set up, reverse it so that we can
487 // remove elements efficiently from the back of the vector.
488 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000489
490 // This is for future proofing... in the future extra fields may be added that
491 // we don't understand, so we transparently ignore them.
492 //
493 Buf = End;
494 return false;
495}
496
497bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Chris Lattner74734132002-08-17 22:01:27 +0000498 Module *&Mod) {
Chris Lattner00950542001-06-06 20:29:01 +0000499
500 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000501 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000502 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
503 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000504 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000505 }
Chris Lattner00950542001-06-06 20:29:01 +0000506
Chris Lattner1d670cc2001-09-07 16:37:43 +0000507 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000508 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000509
510 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000511 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
512 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000513 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000514
Chris Lattner74734132002-08-17 22:01:27 +0000515 TheModule = Mod = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000516
Chris Lattner00950542001-06-06 20:29:01 +0000517 while (Buf < EndBuf) {
518 const uchar *OldBuf = Buf;
Chris Lattner74734132002-08-17 22:01:27 +0000519 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
Chris Lattner00950542001-06-06 20:29:01 +0000520 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000521 case BytecodeFormat::ConstantPool:
522 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
523 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000524 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000525 }
526 break;
527
Chris Lattner1d670cc2001-09-07 16:37:43 +0000528 case BytecodeFormat::ModuleGlobalInfo:
529 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
530
Chris Lattner74734132002-08-17 22:01:27 +0000531 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
532 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000533 }
534 break;
535
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000536 case BytecodeFormat::Function: {
537 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000538 if (ParseMethod(Buf, Buf+Size, Mod)) {
539 delete Mod; return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000540 }
541 break;
542 }
543
544 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000545 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000546 if (ParseSymbolTable(Buf, Buf+Size, Mod->getSymbolTableSure())) {
547 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000548 }
549 break;
550
551 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000552 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000553 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000554 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000555 break;
556 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000557 BCR_TRACE(1, "} end block\n");
Chris Lattner74734132002-08-17 22:01:27 +0000558 if (align32(Buf, EndBuf)) { delete Mod; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000559 }
560
Chris Lattner74734132002-08-17 22:01:27 +0000561 if (!FunctionSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000562 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000563 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000564 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000565
566 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000567 return false;
568}
569
570Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
571 LateResolveValues.clear();
572 unsigned Sig;
573 // Read and check signature...
574 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000575 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
576 Error = "Invalid bytecode signature!";
Chris Lattner74734132002-08-17 22:01:27 +0000577 return 0; // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000578 }
Chris Lattner00950542001-06-06 20:29:01 +0000579
580 Module *Result;
581 if (ParseModule(Buf, EndBuf, Result)) return 0;
582 return Result;
583}
584
585
Anand Shukla4f2d5a42002-09-20 20:57:54 +0000586Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) {
Chris Lattner00950542001-06-06 20:29:01 +0000587 BytecodeParser Parser;
588 return Parser.ParseBytecode(Buffer, Buffer+Length);
589}
590
591// Parse and return a class file...
592//
Chris Lattner697954c2002-01-20 22:54:45 +0000593Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000594 struct stat StatBuf;
595 Module *Result = 0;
596
Chris Lattner697954c2002-01-20 22:54:45 +0000597 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000598 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000599 if (FD == -1) {
600 if (ErrorStr) *ErrorStr = "Error opening file!";
Chris Lattner74734132002-08-17 22:01:27 +0000601 return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000602 }
Chris Lattner00950542001-06-06 20:29:01 +0000603
Chris Lattner74734132002-08-17 22:01:27 +0000604 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000605
606 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000607 if (Length == 0) {
608 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
Chris Lattner74734132002-08-17 22:01:27 +0000609 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000610 }
Chris Lattner00950542001-06-06 20:29:01 +0000611 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
612 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000613 if (Buffer == (uchar*)-1) {
614 if (ErrorStr) *ErrorStr = "Error mmapping file!";
Chris Lattner74734132002-08-17 22:01:27 +0000615 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000616 }
Chris Lattner00950542001-06-06 20:29:01 +0000617
618 BytecodeParser Parser;
619 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
620
621 munmap((char*)Buffer, Length);
622 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000623 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000624 } else { // Read from stdin
625 size_t FileSize = 0;
626 int BlockSize;
627 uchar Buffer[4096], *FileData = 0;
Chris Lattner5dfab9e2002-10-22 23:55:24 +0000628 while ((BlockSize = read(0, Buffer, 4096))) {
Chris Lattner74734132002-08-17 22:01:27 +0000629 if (BlockSize == -1) { free(FileData); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000630
631 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
632 memcpy(FileData+FileSize, Buffer, BlockSize);
633 FileSize += BlockSize;
634 }
635
Chris Lattnerd6b65252001-10-24 01:15:12 +0000636 if (FileSize == 0) {
637 if (ErrorStr) *ErrorStr = "Standard Input empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000638 free(FileData); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000639 }
Chris Lattner00950542001-06-06 20:29:01 +0000640
Chris Lattner915ce8a2002-08-18 00:38:32 +0000641#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000642#if ALIGN_PTRS
643 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
644 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
645 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000646 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000647 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000648#else
649 uchar *Buf = FileData;
650#endif
651
652 BytecodeParser Parser;
653 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
654
655#if ALIGN_PTRS
656 munmap((char*)Buf, FileSize); // Free mmap'd data area
657#else
658 free(FileData); // Free realloc'd block of memory
659#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000660
661 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000662 }
663
664 return Result;
665}