blob: c74d09f0c599e887c966b42320017ca0e0813ec2 [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>
26
27bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
28 if (Ty->isPrimitiveType()) {
29 Slot = Ty->getPrimitiveID();
30 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000031 // Check the method level types first...
32 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
33 MethodTypeValues.end(), Ty);
34 if (I != MethodTypeValues.end()) {
35 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
36 (&*I - &MethodTypeValues[0]);
37 } else {
38 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
39 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
40 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
41 }
Chris Lattner00950542001-06-06 20:29:01 +000042 }
Chris Lattner697954c2002-01-20 22:54:45 +000043 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000044 return false;
45}
46
47const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000048 if (ID < Type::NumPrimitiveIDs) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
50 if (T) return T;
51 }
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattner697954c2002-01-20 22:54:45 +000053 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000054 const Value *V = getValue(Type::TypeTy, ID, false);
55 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
Chris Lattner697954c2002-01-20 22:54:45 +000058int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000059 unsigned type;
Chris Lattner74734132002-08-17 22:01:27 +000060 if (getTypeSlot(Val->getType(), type)) return -1;
Chris Lattner1d670cc2001-09-07 16:37:43 +000061 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000062
63 if (ValueTab.size() <= type)
64 ValueTab.resize(type+1, ValueList());
65
66 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000067 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000068 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner05950c32001-10-13 06:47:01 +000070 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000071}
72
73Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
74 unsigned Num = oNum;
75 unsigned type; // The type plane it lives in...
76
Chris Lattner74734132002-08-17 22:01:27 +000077 if (getTypeSlot(Ty, type)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000078
79 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000080 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +000081 if (Num < Type::NumPrimitiveIDs) {
82 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
83 if (T) return (Value*)T; // Asked for a primitive type...
84 }
Chris Lattner00950542001-06-06 20:29:01 +000085
86 // Otherwise, derived types need offset...
87 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000088
89 // Is it a module level type?
90 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000091 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000092
93 // Nope, is it a method level type?
94 Num -= ModuleTypeValues.size();
95 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000096 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000097
98 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000099 }
100
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 if (type < ModuleValues.size()) {
102 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000103 return ModuleValues[type][Num];
104 Num -= ModuleValues[type].size();
105 }
106
107 if (Values.size() > type && Values[type].size() > Num)
108 return Values[type][Num];
109
Chris Lattner74734132002-08-17 22:01:27 +0000110 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000111
112 Value *d = 0;
113 switch (Ty->getPrimitiveID()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000114 case Type::FunctionTyID:
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000115 std::cerr << "Creating method pholder! : " << type << ":" << oNum << " "
116 << Ty->getName() << "\n";
Chris Lattner74734132002-08-17 22:01:27 +0000117 d = new FunctionPHolder(Ty, oNum);
118 if (insertValue(d, LateResolveModuleValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000119 return d;
Chris Lattner74734132002-08-17 22:01:27 +0000120 case Type::LabelTyID:
121 d = new BBPHolder(Ty, oNum);
122 break;
123 default:
124 d = new ValPHolder(Ty, oNum);
125 break;
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
127
128 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000129 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000130 return d;
131}
132
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000133/// getConstantValue - Just like getValue, except that it returns a null pointer
134/// only on error. It always returns a constant (meaning that if the value is
135/// defined, but is not a constant, that is an error). If the specified
136/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
137/// after the real value is parsed, the placeholder is eliminated.
138///
139Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
140 if (Value *V = getValue(Ty, Slot, false))
141 return dyn_cast<Constant>(V); // If we already have the value parsed...
142
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000143 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(Ty, Slot));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000144 if (I != GlobalRefs.end()) {
145 BCR_TRACE(5, "Previous forward ref found!\n");
146 return cast<Constant>(I->second);
147 } else {
148 // Create a placeholder for the constant reference and
149 // keep track of the fact that we have a forward ref to recycle it
150 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
151 Constant *C = new ConstPHolder(Ty, Slot);
152
153 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000154 GlobalRefs.insert(std::make_pair(std::make_pair(Ty, Slot), C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000155 return C;
156 }
157}
158
159
160
Chris Lattner00950542001-06-06 20:29:01 +0000161bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
162 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000163 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000164 ValueList &DL = ValTab[ty];
165 unsigned Size;
166 while ((Size = DL.size())) {
167 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
168
169 Value *D = DL[Size-1];
170 DL.pop_back();
171
172 Value *NewDef = getValue(D->getType(), IDNumber, false);
173 if (NewDef == 0) {
174 Error = true; // Unresolved thinger
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000175 std::cerr << "Unresolvable reference found: <"
176 << D->getType()->getDescription() << ">:" << IDNumber <<"!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000177 } else {
178 // Fixup all of the uses of this placeholder def...
179 D->replaceAllUsesWith(NewDef);
180
181 // Now that all the uses are gone, delete the placeholder...
182 // If we couldn't find a def (error case), then leak a little
183 delete D; // memory, 'cause otherwise we can't remove all uses!
184 }
185 }
186 }
187
188 return Error;
189}
190
191bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
192 BasicBlock *&BB) {
193 BB = new BasicBlock();
194
195 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000196 Instruction *Inst;
Chris Lattner352eef72002-08-21 22:55:27 +0000197 if (ParseInstruction(Buf, EndBuf, Inst,
198 /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000199 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000200 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000201 }
202
Chris Lattner74734132002-08-17 22:01:27 +0000203 if (Inst == 0) { delete BB; return true; }
204 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000205
Chris Lattner1d670cc2001-09-07 16:37:43 +0000206 BB->getInstList().push_back(Inst);
207
208 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000209 }
210
211 return false;
212}
213
Chris Lattner1d670cc2001-09-07 16:37:43 +0000214bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
215 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000216 while (Buf < EndBuf) {
217 // Symtab block header: [num entries][type id number]
218 unsigned NumEntries, Typ;
219 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000220 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000221 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000222 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000223
Chris Lattner1d670cc2001-09-07 16:37:43 +0000224 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
225 " entries\n");
226
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000227 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000228 // Symtab entry: [def slot #][name]
229 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000230 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000231 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000232 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000233 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000234
235 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000236 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000237 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000238 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000239 }
240 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000241 if (!isa<Instruction>(D)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000242
243 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000244 }
245 }
246
Chris Lattner74734132002-08-17 22:01:27 +0000247 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000248 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000249}
250
Chris Lattner74734132002-08-17 22:01:27 +0000251void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000252 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
253 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000254 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000255
Chris Lattner74734132002-08-17 22:01:27 +0000256 BCR_TRACE(3, "Mutating forward refs!\n");
257 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000258
Chris Lattner74734132002-08-17 22:01:27 +0000259 // Loop over all of the uses of the Value. What they are depends
260 // on what NewV is. Replacing a use of the old reference takes the
261 // use off the use list, so loop with !use_empty(), not the use_iterator.
262 while (!VPH->use_empty()) {
263 Constant *C = cast<Constant>(VPH->use_back());
264 unsigned numReplaced = C->mutateReferences(VPH, NewV);
265 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000266
Chris Lattner74734132002-08-17 22:01:27 +0000267 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
268 // Remove the placeholder GlobalValue from the module...
269 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000270 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000271 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000272
Chris Lattner74734132002-08-17 22:01:27 +0000273 delete VPH; // Delete the old placeholder
274 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000275}
276
Chris Lattner00950542001-06-06 20:29:01 +0000277bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
278 Module *C) {
279 // Clear out the local values table...
280 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000281 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000282 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000283 return true; // Unexpected method!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000284 }
Chris Lattner00950542001-06-06 20:29:01 +0000285
Chris Lattner74734132002-08-17 22:01:27 +0000286 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000287 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000288 if (MTy == 0) return true; // Not ptr to method!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000289
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000290 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000291 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000292
Chris Lattner74734132002-08-17 22:01:27 +0000293 unsigned MethSlot = FunctionSignatureList.back().second;
294 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000295 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000296
Chris Lattner697954c2002-01-20 22:54:45 +0000297 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000298
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000299 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000300 Function::aiterator AI = M->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000301 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000302 It != Params.end(); ++It, ++AI) {
303 if (insertValue(AI, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000304 Error = "Error reading method arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000305 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000306 }
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
308
309 while (Buf < EndBuf) {
310 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000311 const unsigned char *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000312 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000313 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000314 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000315 }
Chris Lattner00950542001-06-06 20:29:01 +0000316
317 switch (Type) {
318 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000319 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
320 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000321 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000322 }
323 break;
324
325 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000326 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000327 BasicBlock *BB;
328 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000329 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000330 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000331 }
332
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000333 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000334 break;
335 }
336
337 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000338 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000339 if (ParseSymbolTable(Buf, Buf+Size, &M->getSymbolTable())) {
Chris Lattner74734132002-08-17 22:01:27 +0000340 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000341 }
342 break;
343
344 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000345 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000346 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000347 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000348 break;
349 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000350 BCR_TRACE(2, "} end block\n");
351
Chris Lattner00950542001-06-06 20:29:01 +0000352 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000353 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000354 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000355 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000356 }
357 }
358
359 if (postResolveValues(LateResolveValues) ||
360 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000361 Error = "Error resolving method values!";
Chris Lattner74734132002-08-17 22:01:27 +0000362 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000363 }
364
Chris Lattner74734132002-08-17 22:01:27 +0000365 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
366 assert(FunctionPHolder && "Something is broken no placeholder found!");
367 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000368
369 unsigned type; // Type slot
370 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000371 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000372
Chris Lattner79df7c02002-03-26 18:01:55 +0000373 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000374
375 // Replace placeholder with the real method pointer...
376 ModuleValues[type][MethSlot] = M;
377
Chris Lattnere4d71a12001-09-14 22:03:42 +0000378 // Clear out method level types...
379 MethodTypeValues.clear();
380
Chris Lattner00950542001-06-06 20:29:01 +0000381 // If anyone is using the placeholder make them use the real method instead
Chris Lattner74734132002-08-17 22:01:27 +0000382 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000383
384 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000385 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000386
Chris Lattner74734132002-08-17 22:01:27 +0000387 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000388
Chris Lattner00950542001-06-06 20:29:01 +0000389 return false;
390}
391
392bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000393 Module *Mod) {
Chris Lattner74734132002-08-17 22:01:27 +0000394 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000395 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000396 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000397 }
Chris Lattner00950542001-06-06 20:29:01 +0000398
Chris Lattner70cc3392001-09-10 07:58:01 +0000399 // Read global variables...
400 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000401 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000402 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000403 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
404 // bit2 = isInternal, bit3+ = slot#
405 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000406 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000407 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000408 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000409 }
410
Chris Lattneref9c23f2001-10-03 14:53:21 +0000411 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000412 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000413
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000414 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000415 if (VarType & 2) { // Does it have an initalizer?
416 // Do not improvise... values must have been stored in the constant pool,
417 // which should have been read before now.
418 //
419 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000420 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000421
Chris Lattner05950c32001-10-13 06:47:01 +0000422 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000423 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000424 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000425 }
426
Chris Lattner70cc3392001-09-10 07:58:01 +0000427 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000428 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
429 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000430 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000431 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000432
433 Mod->getGlobalList().push_back(GV);
434
Chris Lattner74734132002-08-17 22:01:27 +0000435 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000436
437 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000438 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000439
Chris Lattner74734132002-08-17 22:01:27 +0000440 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000441 }
442
Chris Lattner00950542001-06-06 20:29:01 +0000443 // Read the method signatures for all of the methods that are coming, and
444 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000445 unsigned FnSignature;
446 if (read_vbr(Buf, End, FnSignature)) return true;
447 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
448 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000449 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000450 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
451 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000452 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000453 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000454
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000455 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000456 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000457
Chris Lattner1d670cc2001-09-07 16:37:43 +0000458 // When the ModuleGlobalInfo section is read, we load the type of each
459 // method and the 'ModuleValues' slot that it lands in. We then load a
460 // placeholder into its slot to reserve it. When the method is loaded, this
461 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000462
463 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000464 Value *Val = new FunctionPHolder(Ty, 0);
465 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000466
467 // Figure out which entry of its typeslot it went into...
468 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000469 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000470
471 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
472
473 // Keep track of this information in a linked list that is emptied as
474 // methods are loaded...
475 //
Chris Lattner74734132002-08-17 22:01:27 +0000476 FunctionSignatureList.push_back(
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000477 std::make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000478 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000479 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000480 }
481
Chris Lattner74734132002-08-17 22:01:27 +0000482 if (align32(Buf, End)) return true;
483
484 // Now that the function signature list is set up, reverse it so that we can
485 // remove elements efficiently from the back of the vector.
486 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000487
488 // This is for future proofing... in the future extra fields may be added that
489 // we don't understand, so we transparently ignore them.
490 //
491 Buf = End;
492 return false;
493}
494
495bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Chris Lattnerb6c46952003-03-06 17:03:28 +0000496 Module *&Mod) {
Chris Lattner00950542001-06-06 20:29:01 +0000497
498 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000499 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000500 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
501 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000502 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000503 }
Chris Lattner00950542001-06-06 20:29:01 +0000504
Chris Lattner1d670cc2001-09-07 16:37:43 +0000505 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000506 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000507
508 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000509 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
510 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000511 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000512
Chris Lattner74734132002-08-17 22:01:27 +0000513 TheModule = Mod = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000514
Chris Lattner00950542001-06-06 20:29:01 +0000515 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000516 const unsigned char *OldBuf = Buf;
Chris Lattner74734132002-08-17 22:01:27 +0000517 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
Chris Lattner00950542001-06-06 20:29:01 +0000518 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000519 case BytecodeFormat::ConstantPool:
520 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
521 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000522 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000523 }
524 break;
525
Chris Lattner1d670cc2001-09-07 16:37:43 +0000526 case BytecodeFormat::ModuleGlobalInfo:
527 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
528
Chris Lattner74734132002-08-17 22:01:27 +0000529 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
530 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000531 }
532 break;
533
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000534 case BytecodeFormat::Function: {
535 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000536 if (ParseMethod(Buf, Buf+Size, Mod)) {
537 delete Mod; return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000538 }
539 break;
540 }
541
542 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000543 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000544 if (ParseSymbolTable(Buf, Buf+Size, &Mod->getSymbolTable())) {
Chris Lattner74734132002-08-17 22:01:27 +0000545 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000546 }
547 break;
548
549 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000550 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000551 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000552 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000553 break;
554 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000555 BCR_TRACE(1, "} end block\n");
Chris Lattner74734132002-08-17 22:01:27 +0000556 if (align32(Buf, EndBuf)) { delete Mod; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000557 }
558
Chris Lattner74734132002-08-17 22:01:27 +0000559 if (!FunctionSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000560 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000561 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000562 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000563
564 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000565 return false;
566}
567
568Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
569 LateResolveValues.clear();
570 unsigned Sig;
571 // Read and check signature...
572 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000573 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
574 Error = "Invalid bytecode signature!";
Chris Lattner74734132002-08-17 22:01:27 +0000575 return 0; // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000576 }
Chris Lattner00950542001-06-06 20:29:01 +0000577
578 Module *Result;
579 if (ParseModule(Buf, EndBuf, Result)) return 0;
580 return Result;
581}
582
583
Chris Lattner09abe6a2003-03-06 16:50:32 +0000584Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
585 std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000586 BytecodeParser Parser;
Chris Lattner09abe6a2003-03-06 16:50:32 +0000587 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length);
588 if (ErrorStr) *ErrorStr = Parser.getError();
589 return R;
Chris Lattner00950542001-06-06 20:29:01 +0000590}
591
Chris Lattnerb6c46952003-03-06 17:03:28 +0000592
593/// FDHandle - Simple handle class to make sure a file descriptor gets closed
594/// when the object is destroyed.
595class FDHandle {
596 int FD;
597public:
598 FDHandle(int fd) : FD(fd) {}
599 operator int() const { return FD; }
600 ~FDHandle() {
601 if (FD != -1) close(FD);
602 }
603};
604
605static inline Module *Error(std::string *ErrorStr, const char *Message) {
606 if (ErrorStr) *ErrorStr = Message;
607 return 0;
608}
609
Chris Lattner00950542001-06-06 20:29:01 +0000610// Parse and return a class file...
611//
Chris Lattner697954c2002-01-20 22:54:45 +0000612Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000613 Module *Result = 0;
614
Chris Lattner697954c2002-01-20 22:54:45 +0000615 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb6c46952003-03-06 17:03:28 +0000616 FDHandle FD = open(Filename.c_str(), O_RDONLY);
617 if (FD == -1)
618 return Error(ErrorStr, "Error opening file!");
Chris Lattner00950542001-06-06 20:29:01 +0000619
Chris Lattnerb6c46952003-03-06 17:03:28 +0000620 // Stat the file to get its length...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000621 struct stat StatBuf;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000622 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
623 return Error(ErrorStr, "Error stat'ing file!");
Chris Lattner09abe6a2003-03-06 16:50:32 +0000624
Chris Lattnerb6c46952003-03-06 17:03:28 +0000625 // mmap in the file all at once...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000626 int Length = StatBuf.st_size;
627 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
628 MAP_PRIVATE, FD, 0);
Chris Lattnerb6c46952003-03-06 17:03:28 +0000629 if (Buffer == (unsigned char*)MAP_FAILED)
630 return Error(ErrorStr, "Error mmapping file!");
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattnerb6c46952003-03-06 17:03:28 +0000632 // Parse the bytecode we mmapped in
Chris Lattner09abe6a2003-03-06 16:50:32 +0000633 Result = ParseBytecodeBuffer(Buffer, Length, ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattnerb6c46952003-03-06 17:03:28 +0000635 // Unmmap the bytecode...
Chris Lattner00950542001-06-06 20:29:01 +0000636 munmap((char*)Buffer, Length);
Chris Lattner00950542001-06-06 20:29:01 +0000637 } else { // Read from stdin
Chris Lattner00950542001-06-06 20:29:01 +0000638 int BlockSize;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000639 uchar Buffer[4096*4];
640 std::vector<unsigned char> FileData;
Chris Lattner00950542001-06-06 20:29:01 +0000641
Chris Lattnerb6c46952003-03-06 17:03:28 +0000642 // Read in all of the data from stdin, we cannot mmap stdin...
643 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
644 if (BlockSize == -1)
645 return Error(ErrorStr, "Error reading from stdin!");
646
647 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
Chris Lattner00950542001-06-06 20:29:01 +0000648 }
649
Chris Lattnerb6c46952003-03-06 17:03:28 +0000650 if (FileData.empty())
651 return Error(ErrorStr, "Standard Input empty!");
Chris Lattner00950542001-06-06 20:29:01 +0000652
Chris Lattner915ce8a2002-08-18 00:38:32 +0000653#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000654#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000655 uchar *Buf = (uchar*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
Chris Lattner00950542001-06-06 20:29:01 +0000656 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
657 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattnerb6c46952003-03-06 17:03:28 +0000658 memcpy(Buf, &FileData[0], FileData.size());
Chris Lattner00950542001-06-06 20:29:01 +0000659#else
Chris Lattnerb6c46952003-03-06 17:03:28 +0000660 unsigned char *Buf = &FileData[0];
Chris Lattner00950542001-06-06 20:29:01 +0000661#endif
662
Chris Lattnerb6c46952003-03-06 17:03:28 +0000663 Result = ParseBytecodeBuffer(Buf, FileData.size(), ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000664
665#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000666 munmap((char*)Buf, FileData.size()); // Free mmap'd data area
Chris Lattner00950542001-06-06 20:29:01 +0000667#endif
Chris Lattner00950542001-06-06 20:29:01 +0000668 }
669
670 return Result;
671}