blob: 9aa54552110109f5b8896399b8b6a64b280240a5 [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) {
51 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
52 if (T) return T;
53
Chris Lattner697954c2002-01-20 22:54:45 +000054 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000055
56 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner74734132002-08-17 22:01:27 +000057 if (D == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000058
Chris Lattnercfe26c92001-10-01 18:26:53 +000059 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000060}
61
Chris Lattner697954c2002-01-20 22:54:45 +000062int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000063 unsigned type;
Chris Lattner74734132002-08-17 22:01:27 +000064 if (getTypeSlot(Val->getType(), type)) return -1;
Chris Lattner1d670cc2001-09-07 16:37:43 +000065 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000066
67 if (ValueTab.size() <= type)
68 ValueTab.resize(type+1, ValueList());
69
70 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000071 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattner05950c32001-10-13 06:47:01 +000074 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000075}
76
77Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
78 unsigned Num = oNum;
79 unsigned type; // The type plane it lives in...
80
Chris Lattner74734132002-08-17 22:01:27 +000081 if (getTypeSlot(Ty, type)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000082
83 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000084 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000085 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
86 if (T) return (Value*)T; // Asked for a primitive type...
87
88 // Otherwise, derived types need offset...
89 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000090
91 // Is it a module level type?
92 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000093 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000094
95 // Nope, is it a method level type?
96 Num -= ModuleTypeValues.size();
97 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000098 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000099
100 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000101 }
102
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103 if (type < ModuleValues.size()) {
104 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000105 return ModuleValues[type][Num];
106 Num -= ModuleValues[type].size();
107 }
108
109 if (Values.size() > type && Values[type].size() > Num)
110 return Values[type][Num];
111
Chris Lattner74734132002-08-17 22:01:27 +0000112 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000113
114 Value *d = 0;
115 switch (Ty->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000116 case Type::FunctionTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000117 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000118 << Ty->getName() << "\n";
Chris Lattner74734132002-08-17 22:01:27 +0000119 d = new FunctionPHolder(Ty, oNum);
120 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000121 return d;
Chris Lattner74734132002-08-17 22:01:27 +0000122 case Type::LabelTyID:
123 d = new BBPHolder(Ty, oNum);
124 break;
125 default:
126 d = new ValPHolder(Ty, oNum);
127 break;
Chris Lattner00950542001-06-06 20:29:01 +0000128 }
129
130 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000131 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000132 return d;
133}
134
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000135/// getConstantValue - Just like getValue, except that it returns a null pointer
136/// only on error. It always returns a constant (meaning that if the value is
137/// defined, but is not a constant, that is an error). If the specified
138/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
139/// after the real value is parsed, the placeholder is eliminated.
140///
141Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
142 if (Value *V = getValue(Ty, Slot, false))
143 return dyn_cast<Constant>(V); // If we already have the value parsed...
144
145 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot));
146 if (I != GlobalRefs.end()) {
147 BCR_TRACE(5, "Previous forward ref found!\n");
148 return cast<Constant>(I->second);
149 } else {
150 // Create a placeholder for the constant reference and
151 // keep track of the fact that we have a forward ref to recycle it
152 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
153 Constant *C = new ConstPHolder(Ty, Slot);
154
155 // Keep track of the fact that we have a forward ref to recycle it
156 GlobalRefs.insert(make_pair(make_pair(Ty, Slot), C));
157 return C;
158 }
159}
160
161
162
Chris Lattner00950542001-06-06 20:29:01 +0000163bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
164 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000165 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000166 ValueList &DL = ValTab[ty];
167 unsigned Size;
168 while ((Size = DL.size())) {
169 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
170
171 Value *D = DL[Size-1];
172 DL.pop_back();
173
174 Value *NewDef = getValue(D->getType(), IDNumber, false);
175 if (NewDef == 0) {
176 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000177 cerr << "Unresolvable reference found: <"
178 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000179 } else {
180 // Fixup all of the uses of this placeholder def...
181 D->replaceAllUsesWith(NewDef);
182
183 // Now that all the uses are gone, delete the placeholder...
184 // If we couldn't find a def (error case), then leak a little
185 delete D; // memory, 'cause otherwise we can't remove all uses!
186 }
187 }
188 }
189
190 return Error;
191}
192
193bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
194 BasicBlock *&BB) {
195 BB = new BasicBlock();
196
197 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000198 Instruction *Inst;
Chris Lattner352eef72002-08-21 22:55:27 +0000199 if (ParseInstruction(Buf, EndBuf, Inst,
200 /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000201 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000202 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000203 }
204
Chris Lattner74734132002-08-17 22:01:27 +0000205 if (Inst == 0) { delete BB; return true; }
206 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000207
Chris Lattner1d670cc2001-09-07 16:37:43 +0000208 BB->getInstList().push_back(Inst);
209
210 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000211 }
212
213 return false;
214}
215
Chris Lattner1d670cc2001-09-07 16:37:43 +0000216bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
217 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000218 while (Buf < EndBuf) {
219 // Symtab block header: [num entries][type id number]
220 unsigned NumEntries, Typ;
221 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000222 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000223 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000224 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000225
Chris Lattner1d670cc2001-09-07 16:37:43 +0000226 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
227 " entries\n");
228
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000229 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000230 // Symtab entry: [def slot #][name]
231 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000232 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000233 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000234 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000235 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000236
237 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000238 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000239 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000240 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000241 }
242 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000243 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000244
245 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000246 }
247 }
248
Chris Lattner74734132002-08-17 22:01:27 +0000249 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000250 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000251}
252
Chris Lattner74734132002-08-17 22:01:27 +0000253void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
254 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(NewV->getType(),Slot));
255 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000256
Chris Lattner74734132002-08-17 22:01:27 +0000257 BCR_TRACE(3, "Mutating forward refs!\n");
258 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000259
Chris Lattner74734132002-08-17 22:01:27 +0000260 // Loop over all of the uses of the Value. What they are depends
261 // on what NewV is. Replacing a use of the old reference takes the
262 // use off the use list, so loop with !use_empty(), not the use_iterator.
263 while (!VPH->use_empty()) {
264 Constant *C = cast<Constant>(VPH->use_back());
265 unsigned numReplaced = C->mutateReferences(VPH, NewV);
266 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000267
Chris Lattner74734132002-08-17 22:01:27 +0000268 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
269 // Remove the placeholder GlobalValue from the module...
270 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000271 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000272 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000273
Chris Lattner74734132002-08-17 22:01:27 +0000274 delete VPH; // Delete the old placeholder
275 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000276}
277
Chris Lattner00950542001-06-06 20:29:01 +0000278bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
279 Module *C) {
280 // Clear out the local values table...
281 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000282 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000283 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000284 return true; // Unexpected method!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000285 }
Chris Lattner00950542001-06-06 20:29:01 +0000286
Chris Lattner74734132002-08-17 22:01:27 +0000287 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000288 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000289 if (MTy == 0) return true; // Not ptr to method!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000290
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000291 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000292 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000293
Chris Lattner74734132002-08-17 22:01:27 +0000294 unsigned MethSlot = FunctionSignatureList.back().second;
295 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000296 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000297
Chris Lattner697954c2002-01-20 22:54:45 +0000298 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000299
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000300 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000301 Function::aiterator AI = M->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000302 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000303 It != Params.end(); ++It, ++AI) {
304 if (insertValue(AI, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000305 Error = "Error reading method arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000306 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000307 }
Chris Lattner00950542001-06-06 20:29:01 +0000308 }
309
310 while (Buf < EndBuf) {
311 unsigned Type, Size;
312 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000313 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000314 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000315 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000316 }
Chris Lattner00950542001-06-06 20:29:01 +0000317
318 switch (Type) {
319 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000320 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
321 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000322 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000323 }
324 break;
325
326 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000327 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000328 BasicBlock *BB;
329 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000330 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000331 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000332 }
333
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000334 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000335 break;
336 }
337
338 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000339 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
340 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner74734132002-08-17 22:01:27 +0000341 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000342 }
343 break;
344
345 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000346 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000347 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000348 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000349 break;
350 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000351 BCR_TRACE(2, "} end block\n");
352
Chris Lattner00950542001-06-06 20:29:01 +0000353 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000354 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000355 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000356 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000357 }
358 }
359
360 if (postResolveValues(LateResolveValues) ||
361 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000362 Error = "Error resolving method values!";
Chris Lattner74734132002-08-17 22:01:27 +0000363 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000364 }
365
Chris Lattner74734132002-08-17 22:01:27 +0000366 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
367 assert(FunctionPHolder && "Something is broken no placeholder found!");
368 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000369
370 unsigned type; // Type slot
371 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000372 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000373
Chris Lattner79df7c02002-03-26 18:01:55 +0000374 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000375
376 // Replace placeholder with the real method pointer...
377 ModuleValues[type][MethSlot] = M;
378
Chris Lattnere4d71a12001-09-14 22:03:42 +0000379 // Clear out method level types...
380 MethodTypeValues.clear();
381
Chris Lattner00950542001-06-06 20:29:01 +0000382 // If anyone is using the placeholder make them use the real method instead
Chris Lattner74734132002-08-17 22:01:27 +0000383 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000384
385 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000386 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000387
Chris Lattner74734132002-08-17 22:01:27 +0000388 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000389
Chris Lattner00950542001-06-06 20:29:01 +0000390 return false;
391}
392
393bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000394 Module *Mod) {
Chris Lattner74734132002-08-17 22:01:27 +0000395 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000396 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000397 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000398 }
Chris Lattner00950542001-06-06 20:29:01 +0000399
Chris Lattner70cc3392001-09-10 07:58:01 +0000400 // Read global variables...
401 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000402 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000403 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000404 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
405 // bit2 = isInternal, bit3+ = slot#
406 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000407 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000408 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000409 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000410 }
411
Chris Lattneref9c23f2001-10-03 14:53:21 +0000412 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000413 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000414
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000415 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000416 if (VarType & 2) { // Does it have an initalizer?
417 // Do not improvise... values must have been stored in the constant pool,
418 // which should have been read before now.
419 //
420 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000421 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000422
Chris Lattner05950c32001-10-13 06:47:01 +0000423 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000424 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000425 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000426 }
427
Chris Lattner70cc3392001-09-10 07:58:01 +0000428 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000429 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
430 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000431 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000432 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000433
434 Mod->getGlobalList().push_back(GV);
435
Chris Lattner74734132002-08-17 22:01:27 +0000436 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000437
438 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000439 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000440
Chris Lattner74734132002-08-17 22:01:27 +0000441 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000442 }
443
Chris Lattner00950542001-06-06 20:29:01 +0000444 // Read the method signatures for all of the methods that are coming, and
445 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000446 unsigned FnSignature;
447 if (read_vbr(Buf, End, FnSignature)) return true;
448 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
449 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000450 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000451 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
452 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000453 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000454 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000455
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000456 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000457 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000458
Chris Lattner1d670cc2001-09-07 16:37:43 +0000459 // When the ModuleGlobalInfo section is read, we load the type of each
460 // method and the 'ModuleValues' slot that it lands in. We then load a
461 // placeholder into its slot to reserve it. When the method is loaded, this
462 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000463
464 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000465 Value *Val = new FunctionPHolder(Ty, 0);
466 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000467
468 // Figure out which entry of its typeslot it went into...
469 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000470 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000471
472 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
473
474 // Keep track of this information in a linked list that is emptied as
475 // methods are loaded...
476 //
Chris Lattner74734132002-08-17 22:01:27 +0000477 FunctionSignatureList.push_back(
Chris Lattneref9c23f2001-10-03 14:53:21 +0000478 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000479 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000480 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000481 }
482
Chris Lattner74734132002-08-17 22:01:27 +0000483 if (align32(Buf, End)) return true;
484
485 // Now that the function signature list is set up, reverse it so that we can
486 // remove elements efficiently from the back of the vector.
487 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000488
489 // This is for future proofing... in the future extra fields may be added that
490 // we don't understand, so we transparently ignore them.
491 //
492 Buf = End;
493 return false;
494}
495
496bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Chris Lattner74734132002-08-17 22:01:27 +0000497 Module *&Mod) {
Chris Lattner00950542001-06-06 20:29:01 +0000498
499 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000500 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000501 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
502 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000503 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000504 }
Chris Lattner00950542001-06-06 20:29:01 +0000505
Chris Lattner1d670cc2001-09-07 16:37:43 +0000506 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000507 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000508
509 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000510 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
511 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000512 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000513
Chris Lattner74734132002-08-17 22:01:27 +0000514 TheModule = Mod = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000515
Chris Lattner00950542001-06-06 20:29:01 +0000516 while (Buf < EndBuf) {
517 const uchar *OldBuf = Buf;
Chris Lattner74734132002-08-17 22:01:27 +0000518 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
Chris Lattner00950542001-06-06 20:29:01 +0000519 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000520 case BytecodeFormat::ConstantPool:
521 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
522 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000523 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000524 }
525 break;
526
Chris Lattner1d670cc2001-09-07 16:37:43 +0000527 case BytecodeFormat::ModuleGlobalInfo:
528 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
529
Chris Lattner74734132002-08-17 22:01:27 +0000530 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
531 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000532 }
533 break;
534
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000535 case BytecodeFormat::Function: {
536 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000537 if (ParseMethod(Buf, Buf+Size, Mod)) {
538 delete Mod; return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000539 }
540 break;
541 }
542
543 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000544 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000545 if (ParseSymbolTable(Buf, Buf+Size, Mod->getSymbolTableSure())) {
546 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000547 }
548 break;
549
550 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000551 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000552 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000553 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000554 break;
555 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000556 BCR_TRACE(1, "} end block\n");
Chris Lattner74734132002-08-17 22:01:27 +0000557 if (align32(Buf, EndBuf)) { delete Mod; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000558 }
559
Chris Lattner74734132002-08-17 22:01:27 +0000560 if (!FunctionSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000561 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000562 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000563 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000564
565 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000566 return false;
567}
568
569Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
570 LateResolveValues.clear();
571 unsigned Sig;
572 // Read and check signature...
573 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000574 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
575 Error = "Invalid bytecode signature!";
Chris Lattner74734132002-08-17 22:01:27 +0000576 return 0; // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000577 }
Chris Lattner00950542001-06-06 20:29:01 +0000578
579 Module *Result;
580 if (ParseModule(Buf, EndBuf, Result)) return 0;
581 return Result;
582}
583
584
Anand Shukla4f2d5a42002-09-20 20:57:54 +0000585Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) {
Chris Lattner00950542001-06-06 20:29:01 +0000586 BytecodeParser Parser;
587 return Parser.ParseBytecode(Buffer, Buffer+Length);
588}
589
590// Parse and return a class file...
591//
Chris Lattner697954c2002-01-20 22:54:45 +0000592Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000593 struct stat StatBuf;
594 Module *Result = 0;
595
Chris Lattner697954c2002-01-20 22:54:45 +0000596 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000597 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000598 if (FD == -1) {
599 if (ErrorStr) *ErrorStr = "Error opening file!";
Chris Lattner74734132002-08-17 22:01:27 +0000600 return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000601 }
Chris Lattner00950542001-06-06 20:29:01 +0000602
Chris Lattner74734132002-08-17 22:01:27 +0000603 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000604
605 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000606 if (Length == 0) {
607 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
Chris Lattner74734132002-08-17 22:01:27 +0000608 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000609 }
Chris Lattner00950542001-06-06 20:29:01 +0000610 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
611 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000612 if (Buffer == (uchar*)-1) {
613 if (ErrorStr) *ErrorStr = "Error mmapping file!";
Chris Lattner74734132002-08-17 22:01:27 +0000614 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000615 }
Chris Lattner00950542001-06-06 20:29:01 +0000616
617 BytecodeParser Parser;
618 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
619
620 munmap((char*)Buffer, Length);
621 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000622 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000623 } else { // Read from stdin
624 size_t FileSize = 0;
625 int BlockSize;
626 uchar Buffer[4096], *FileData = 0;
627 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner74734132002-08-17 22:01:27 +0000628 if (BlockSize == -1) { free(FileData); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000629
630 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
631 memcpy(FileData+FileSize, Buffer, BlockSize);
632 FileSize += BlockSize;
633 }
634
Chris Lattnerd6b65252001-10-24 01:15:12 +0000635 if (FileSize == 0) {
636 if (ErrorStr) *ErrorStr = "Standard Input empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000637 free(FileData); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000638 }
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner915ce8a2002-08-18 00:38:32 +0000640#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000641#if ALIGN_PTRS
642 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
643 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
644 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000645 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000646 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000647#else
648 uchar *Buf = FileData;
649#endif
650
651 BytecodeParser Parser;
652 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
653
654#if ALIGN_PTRS
655 munmap((char*)Buf, FileSize); // Free mmap'd data area
656#else
657 free(FileData); // Free realloc'd block of memory
658#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000659
660 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000661 }
662
663 return Result;
664}