blob: eb4e9e102e84789e83c17b8f8b099c53cec0c974 [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 Lattner2a7b6ba2003-03-06 17:15:19 +000031 // Check the function level types first...
Chris Lattner1d670cc2001-09-07 16:37:43 +000032 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
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000093 // Nope, is it a function level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +000094 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 Lattner2a7b6ba2003-03-06 17:15:19 +0000115 std::cerr << "Creating function pholder! : " << type << ":" << oNum << " "
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000116 << 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 Lattner2a7b6ba2003-03-06 17:15:19 +0000277bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000278 // Clear out the local values table...
279 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000280 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000281 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000282 return true; // Unexpected function!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000283 }
Chris Lattner00950542001-06-06 20:29:01 +0000284
Chris Lattner74734132002-08-17 22:01:27 +0000285 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000286 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000287 if (MTy == 0) return true; // Not ptr to function!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000288
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000289 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000290 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000291
Chris Lattner74734132002-08-17 22:01:27 +0000292 unsigned MethSlot = FunctionSignatureList.back().second;
293 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000294 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000295
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000296 BCR_TRACE(2, "FUNCTION TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000297
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000298 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000299 Function::aiterator AI = M->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000300 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000301 It != Params.end(); ++It, ++AI) {
302 if (insertValue(AI, Values) == -1) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000303 Error = "Error reading function arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000304 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000305 }
Chris Lattner00950542001-06-06 20:29:01 +0000306 }
307
308 while (Buf < EndBuf) {
309 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000310 const unsigned char *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000311 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000312 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000313 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000314 }
Chris Lattner00950542001-06-06 20:29:01 +0000315
316 switch (Type) {
317 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000318 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
319 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000320 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000321 }
322 break;
323
324 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000325 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000326 BasicBlock *BB;
327 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000328 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000329 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000330 }
331
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000332 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000333 break;
334 }
335
336 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000337 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000338 if (ParseSymbolTable(Buf, Buf+Size, &M->getSymbolTable())) {
Chris Lattner74734132002-08-17 22:01:27 +0000339 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000340 }
341 break;
342
343 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000344 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000345 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000346 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000347 break;
348 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000349 BCR_TRACE(2, "} end block\n");
350
Chris Lattner00950542001-06-06 20:29:01 +0000351 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000352 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000353 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000354 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000355 }
356 }
357
358 if (postResolveValues(LateResolveValues) ||
359 postResolveValues(LateResolveModuleValues)) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000360 Error = "Error resolving function values!";
Chris Lattner74734132002-08-17 22:01:27 +0000361 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000362 }
363
Chris Lattner74734132002-08-17 22:01:27 +0000364 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
365 assert(FunctionPHolder && "Something is broken no placeholder found!");
366 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000367
368 unsigned type; // Type slot
369 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000370 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000371
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000372 TheModule->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000373
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000374 // Replace placeholder with the real function pointer...
Chris Lattner00950542001-06-06 20:29:01 +0000375 ModuleValues[type][MethSlot] = M;
376
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000377 // Clear out function level types...
Chris Lattnere4d71a12001-09-14 22:03:42 +0000378 MethodTypeValues.clear();
379
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000380 // If anyone is using the placeholder make them use the real function instead
Chris Lattner74734132002-08-17 22:01:27 +0000381 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000382
383 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000384 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000385
Chris Lattner74734132002-08-17 22:01:27 +0000386 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000387
Chris Lattner00950542001-06-06 20:29:01 +0000388 return false;
389}
390
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000391bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
Chris Lattner74734132002-08-17 22:01:27 +0000392 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000393 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000394 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000395 }
Chris Lattner00950542001-06-06 20:29:01 +0000396
Chris Lattner70cc3392001-09-10 07:58:01 +0000397 // Read global variables...
398 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000399 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000400 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000401 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
402 // bit2 = isInternal, bit3+ = slot#
403 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000404 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000405 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000406 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000407 }
408
Chris Lattneref9c23f2001-10-03 14:53:21 +0000409 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000410 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000411
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000412 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000413 if (VarType & 2) { // Does it have an initalizer?
414 // Do not improvise... values must have been stored in the constant pool,
415 // which should have been read before now.
416 //
417 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000418 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000419
Chris Lattner05950c32001-10-13 06:47:01 +0000420 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000421 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000422 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000423 }
424
Chris Lattner70cc3392001-09-10 07:58:01 +0000425 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000426 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
427 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000428 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000429 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000430
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000431 TheModule->getGlobalList().push_back(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000432
Chris Lattner74734132002-08-17 22:01:27 +0000433 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000434
435 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000436 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000437
Chris Lattner74734132002-08-17 22:01:27 +0000438 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000439 }
440
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000441 // Read the function signatures for all of the functions that are coming, and
Chris Lattner00950542001-06-06 20:29:01 +0000442 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000443 unsigned FnSignature;
444 if (read_vbr(Buf, End, FnSignature)) return true;
445 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
446 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000447 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000448 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
449 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000450 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000451 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000452
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000453 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000454 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000455
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000456 // When the ModuleGlobalInfo section is read, we load the type of each
457 // function and the 'ModuleValues' slot that it lands in. We then load a
458 // placeholder into its slot to reserve it. When the function is loaded,
459 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000460
461 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000462 Value *Val = new FunctionPHolder(Ty, 0);
463 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000464
465 // Figure out which entry of its typeslot it went into...
466 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000467 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000468
469 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
470
471 // Keep track of this information in a linked list that is emptied as
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000472 // functions are loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000473 //
Chris Lattner74734132002-08-17 22:01:27 +0000474 FunctionSignatureList.push_back(
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000475 std::make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000476 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000477 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000478 }
479
Chris Lattner74734132002-08-17 22:01:27 +0000480 if (align32(Buf, End)) return true;
481
482 // Now that the function signature list is set up, reverse it so that we can
483 // remove elements efficiently from the back of the vector.
484 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000485
486 // This is for future proofing... in the future extra fields may be added that
487 // we don't understand, so we transparently ignore them.
488 //
489 Buf = End;
490 return false;
491}
492
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000493bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000494 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000495 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000496 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
497 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000498 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000499 }
Chris Lattner00950542001-06-06 20:29:01 +0000500
Chris Lattner1d670cc2001-09-07 16:37:43 +0000501 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000502 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000503
504 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000505 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
506 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000507 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000508
Chris Lattner00950542001-06-06 20:29:01 +0000509 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000510 const unsigned char *OldBuf = Buf;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000511 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000512 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000513 case BytecodeFormat::ConstantPool:
514 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000515 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues))
516 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000517 break;
518
Chris Lattner1d670cc2001-09-07 16:37:43 +0000519 case BytecodeFormat::ModuleGlobalInfo:
520 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000521 if (ParseModuleGlobalInfo(Buf, Buf+Size)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000522 break;
523
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000524 case BytecodeFormat::Function: {
525 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000526 if (ParseMethod(Buf, Buf+Size)) return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000527 break;
528 }
529
530 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000531 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000532 if (ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()))
533 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000534 break;
535
536 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000537 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000538 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000539 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000540 break;
541 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000542 BCR_TRACE(1, "} end block\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000543 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000544 }
545
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000546 if (!FunctionSignatureList.empty()) { // Expected more functions!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000547 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000548 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000549 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000550
551 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000552 return false;
553}
554
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000555static inline Module *Error(std::string *ErrorStr, const char *Message) {
556 if (ErrorStr) *ErrorStr = Message;
557 return 0;
558}
559
Chris Lattner00950542001-06-06 20:29:01 +0000560Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
561 LateResolveValues.clear();
562 unsigned Sig;
563 // Read and check signature...
564 if (read(Buf, EndBuf, Sig) ||
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000565 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
566 return ::Error(&Error, "Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000567
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000568 TheModule = new Module();
569 if (ParseModule(Buf, EndBuf)) {
570 delete TheModule;
571 TheModule = 0;
572 }
573 return TheModule;
Chris Lattner00950542001-06-06 20:29:01 +0000574}
575
576
Chris Lattner09abe6a2003-03-06 16:50:32 +0000577Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
578 std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000579 BytecodeParser Parser;
Chris Lattner09abe6a2003-03-06 16:50:32 +0000580 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length);
581 if (ErrorStr) *ErrorStr = Parser.getError();
582 return R;
Chris Lattner00950542001-06-06 20:29:01 +0000583}
584
Chris Lattnerb6c46952003-03-06 17:03:28 +0000585
586/// FDHandle - Simple handle class to make sure a file descriptor gets closed
587/// when the object is destroyed.
588class FDHandle {
589 int FD;
590public:
591 FDHandle(int fd) : FD(fd) {}
592 operator int() const { return FD; }
593 ~FDHandle() {
594 if (FD != -1) close(FD);
595 }
596};
597
Chris Lattner00950542001-06-06 20:29:01 +0000598// Parse and return a class file...
599//
Chris Lattner697954c2002-01-20 22:54:45 +0000600Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000601 Module *Result = 0;
602
Chris Lattner697954c2002-01-20 22:54:45 +0000603 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb6c46952003-03-06 17:03:28 +0000604 FDHandle FD = open(Filename.c_str(), O_RDONLY);
605 if (FD == -1)
606 return Error(ErrorStr, "Error opening file!");
Chris Lattner00950542001-06-06 20:29:01 +0000607
Chris Lattnerb6c46952003-03-06 17:03:28 +0000608 // Stat the file to get its length...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000609 struct stat StatBuf;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000610 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
611 return Error(ErrorStr, "Error stat'ing file!");
Chris Lattner09abe6a2003-03-06 16:50:32 +0000612
Chris Lattnerb6c46952003-03-06 17:03:28 +0000613 // mmap in the file all at once...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000614 int Length = StatBuf.st_size;
615 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
616 MAP_PRIVATE, FD, 0);
Chris Lattnerb6c46952003-03-06 17:03:28 +0000617 if (Buffer == (unsigned char*)MAP_FAILED)
618 return Error(ErrorStr, "Error mmapping file!");
Chris Lattner00950542001-06-06 20:29:01 +0000619
Chris Lattnerb6c46952003-03-06 17:03:28 +0000620 // Parse the bytecode we mmapped in
Chris Lattner09abe6a2003-03-06 16:50:32 +0000621 Result = ParseBytecodeBuffer(Buffer, Length, ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattnerb6c46952003-03-06 17:03:28 +0000623 // Unmmap the bytecode...
Chris Lattner00950542001-06-06 20:29:01 +0000624 munmap((char*)Buffer, Length);
Chris Lattner00950542001-06-06 20:29:01 +0000625 } else { // Read from stdin
Chris Lattner00950542001-06-06 20:29:01 +0000626 int BlockSize;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000627 uchar Buffer[4096*4];
628 std::vector<unsigned char> FileData;
Chris Lattner00950542001-06-06 20:29:01 +0000629
Chris Lattnerb6c46952003-03-06 17:03:28 +0000630 // Read in all of the data from stdin, we cannot mmap stdin...
631 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
632 if (BlockSize == -1)
633 return Error(ErrorStr, "Error reading from stdin!");
634
635 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
Chris Lattner00950542001-06-06 20:29:01 +0000636 }
637
Chris Lattnerb6c46952003-03-06 17:03:28 +0000638 if (FileData.empty())
639 return Error(ErrorStr, "Standard Input empty!");
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
Chris Lattnerb6c46952003-03-06 17:03:28 +0000643 uchar *Buf = (uchar*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
Chris Lattner00950542001-06-06 20:29:01 +0000644 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
645 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattnerb6c46952003-03-06 17:03:28 +0000646 memcpy(Buf, &FileData[0], FileData.size());
Chris Lattner00950542001-06-06 20:29:01 +0000647#else
Chris Lattnerb6c46952003-03-06 17:03:28 +0000648 unsigned char *Buf = &FileData[0];
Chris Lattner00950542001-06-06 20:29:01 +0000649#endif
650
Chris Lattnerb6c46952003-03-06 17:03:28 +0000651 Result = ParseBytecodeBuffer(Buf, FileData.size(), ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000652
653#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000654 munmap((char*)Buf, FileData.size()); // Free mmap'd data area
Chris Lattner00950542001-06-06 20:29:01 +0000655#endif
Chris Lattner00950542001-06-06 20:29:01 +0000656 }
657
658 return Result;
659}