blob: 22b15924d263113f1e93eb2bd50f8c148910f1a1 [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
135bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
136 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000137 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000138 ValueList &DL = ValTab[ty];
139 unsigned Size;
140 while ((Size = DL.size())) {
141 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
142
143 Value *D = DL[Size-1];
144 DL.pop_back();
145
146 Value *NewDef = getValue(D->getType(), IDNumber, false);
147 if (NewDef == 0) {
148 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000149 cerr << "Unresolvable reference found: <"
150 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000151 } else {
152 // Fixup all of the uses of this placeholder def...
153 D->replaceAllUsesWith(NewDef);
154
155 // Now that all the uses are gone, delete the placeholder...
156 // If we couldn't find a def (error case), then leak a little
157 delete D; // memory, 'cause otherwise we can't remove all uses!
158 }
159 }
160 }
161
162 return Error;
163}
164
165bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
166 BasicBlock *&BB) {
167 BB = new BasicBlock();
168
169 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000170 Instruction *Inst;
Chris Lattner352eef72002-08-21 22:55:27 +0000171 if (ParseInstruction(Buf, EndBuf, Inst,
172 /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000173 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000174 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000175 }
176
Chris Lattner74734132002-08-17 22:01:27 +0000177 if (Inst == 0) { delete BB; return true; }
178 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000179
Chris Lattner1d670cc2001-09-07 16:37:43 +0000180 BB->getInstList().push_back(Inst);
181
182 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000183 }
184
185 return false;
186}
187
Chris Lattner1d670cc2001-09-07 16:37:43 +0000188bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
189 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000190 while (Buf < EndBuf) {
191 // Symtab block header: [num entries][type id number]
192 unsigned NumEntries, Typ;
193 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000194 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000195 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000196 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattner1d670cc2001-09-07 16:37:43 +0000198 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
199 " entries\n");
200
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000201 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000202 // Symtab entry: [def slot #][name]
203 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000204 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000205 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000206 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000207 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000208
209 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000210 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000211 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000212 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000213 }
214 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000215 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000216
217 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000218 }
219 }
220
Chris Lattner74734132002-08-17 22:01:27 +0000221 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000222 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000223}
224
Chris Lattner74734132002-08-17 22:01:27 +0000225void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
226 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(NewV->getType(),Slot));
227 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000228
Chris Lattner74734132002-08-17 22:01:27 +0000229 BCR_TRACE(3, "Mutating forward refs!\n");
230 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000231
Chris Lattner74734132002-08-17 22:01:27 +0000232 // Loop over all of the uses of the Value. What they are depends
233 // on what NewV is. Replacing a use of the old reference takes the
234 // use off the use list, so loop with !use_empty(), not the use_iterator.
235 while (!VPH->use_empty()) {
236 Constant *C = cast<Constant>(VPH->use_back());
237 unsigned numReplaced = C->mutateReferences(VPH, NewV);
238 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000239
Chris Lattner74734132002-08-17 22:01:27 +0000240 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
241 // Remove the placeholder GlobalValue from the module...
242 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000243 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000244 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000245
Chris Lattner74734132002-08-17 22:01:27 +0000246 delete VPH; // Delete the old placeholder
247 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000248}
249
Chris Lattner00950542001-06-06 20:29:01 +0000250bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
251 Module *C) {
252 // Clear out the local values table...
253 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000254 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000255 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000256 return true; // Unexpected method!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000257 }
Chris Lattner00950542001-06-06 20:29:01 +0000258
Chris Lattner74734132002-08-17 22:01:27 +0000259 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000260 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000261 if (MTy == 0) return true; // Not ptr to method!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000262
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000263 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000264 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000265
Chris Lattner74734132002-08-17 22:01:27 +0000266 unsigned MethSlot = FunctionSignatureList.back().second;
267 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000268 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000269
Chris Lattner697954c2002-01-20 22:54:45 +0000270 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000271
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000272 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
273 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000274 It != Params.end(); ++It) {
Chris Lattner73e21422002-04-09 19:48:49 +0000275 Argument *FA = new Argument(*It);
Chris Lattner79df7c02002-03-26 18:01:55 +0000276 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000277 Error = "Error reading method arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000278 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000279 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000281 }
282
283 while (Buf < EndBuf) {
284 unsigned Type, Size;
285 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000286 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000287 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000288 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000289 }
Chris Lattner00950542001-06-06 20:29:01 +0000290
291 switch (Type) {
292 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000293 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
294 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000295 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000296 }
297 break;
298
299 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000301 BasicBlock *BB;
302 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000303 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000304 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000305 }
306
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000307 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000308 break;
309 }
310
311 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000312 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
313 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner74734132002-08-17 22:01:27 +0000314 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000315 }
316 break;
317
318 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000319 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000320 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000321 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000322 break;
323 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000324 BCR_TRACE(2, "} end block\n");
325
Chris Lattner00950542001-06-06 20:29:01 +0000326 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000327 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000328 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000329 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000330 }
331 }
332
333 if (postResolveValues(LateResolveValues) ||
334 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000335 Error = "Error resolving method values!";
Chris Lattner74734132002-08-17 22:01:27 +0000336 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000337 }
338
Chris Lattner74734132002-08-17 22:01:27 +0000339 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
340 assert(FunctionPHolder && "Something is broken no placeholder found!");
341 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000342
343 unsigned type; // Type slot
344 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000345 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000346
Chris Lattner79df7c02002-03-26 18:01:55 +0000347 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000348
349 // Replace placeholder with the real method pointer...
350 ModuleValues[type][MethSlot] = M;
351
Chris Lattnere4d71a12001-09-14 22:03:42 +0000352 // Clear out method level types...
353 MethodTypeValues.clear();
354
Chris Lattner00950542001-06-06 20:29:01 +0000355 // If anyone is using the placeholder make them use the real method instead
Chris Lattner74734132002-08-17 22:01:27 +0000356 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000357
358 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000359 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000360
Chris Lattnerb847f512001-10-14 23:28:41 +0000361 // If the method is empty, we don't need the method argument entries...
362 if (M->isExternal())
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000363 M->getArgumentList().clear();
Chris Lattnerb847f512001-10-14 23:28:41 +0000364
Chris Lattner74734132002-08-17 22:01:27 +0000365 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000366
Chris Lattner00950542001-06-06 20:29:01 +0000367 return false;
368}
369
370bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000371 Module *Mod) {
Chris Lattner74734132002-08-17 22:01:27 +0000372 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000373 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000374 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000375 }
Chris Lattner00950542001-06-06 20:29:01 +0000376
Chris Lattner70cc3392001-09-10 07:58:01 +0000377 // Read global variables...
378 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000379 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000380 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000381 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
382 // bit2 = isInternal, bit3+ = slot#
383 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000384 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000385 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000386 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000387 }
388
Chris Lattneref9c23f2001-10-03 14:53:21 +0000389 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000390 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000391
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000392 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000393 if (VarType & 2) { // Does it have an initalizer?
394 // Do not improvise... values must have been stored in the constant pool,
395 // which should have been read before now.
396 //
397 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000398 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000399
Chris Lattner05950c32001-10-13 06:47:01 +0000400 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000401 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000402 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000403 }
404
Chris Lattner70cc3392001-09-10 07:58:01 +0000405 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000406 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
407 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000408 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000409 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000410
411 Mod->getGlobalList().push_back(GV);
412
Chris Lattner74734132002-08-17 22:01:27 +0000413 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000414
415 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000416 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000417
Chris Lattner74734132002-08-17 22:01:27 +0000418 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000419 }
420
Chris Lattner00950542001-06-06 20:29:01 +0000421 // Read the method signatures for all of the methods that are coming, and
422 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000423 unsigned FnSignature;
424 if (read_vbr(Buf, End, FnSignature)) return true;
425 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
426 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000427 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000428 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
429 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000430 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000431 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000432
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000433 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000434 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000435
Chris Lattner1d670cc2001-09-07 16:37:43 +0000436 // When the ModuleGlobalInfo section is read, we load the type of each
437 // method and the 'ModuleValues' slot that it lands in. We then load a
438 // placeholder into its slot to reserve it. When the method is loaded, this
439 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000440
441 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000442 Value *Val = new FunctionPHolder(Ty, 0);
443 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000444
445 // Figure out which entry of its typeslot it went into...
446 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000447 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000448
449 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
450
451 // Keep track of this information in a linked list that is emptied as
452 // methods are loaded...
453 //
Chris Lattner74734132002-08-17 22:01:27 +0000454 FunctionSignatureList.push_back(
Chris Lattneref9c23f2001-10-03 14:53:21 +0000455 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000456 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000457 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000458 }
459
Chris Lattner74734132002-08-17 22:01:27 +0000460 if (align32(Buf, End)) return true;
461
462 // Now that the function signature list is set up, reverse it so that we can
463 // remove elements efficiently from the back of the vector.
464 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000465
466 // This is for future proofing... in the future extra fields may be added that
467 // we don't understand, so we transparently ignore them.
468 //
469 Buf = End;
470 return false;
471}
472
473bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Chris Lattner74734132002-08-17 22:01:27 +0000474 Module *&Mod) {
Chris Lattner00950542001-06-06 20:29:01 +0000475
476 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000477 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000478 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
479 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000480 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000481 }
Chris Lattner00950542001-06-06 20:29:01 +0000482
Chris Lattner1d670cc2001-09-07 16:37:43 +0000483 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000484 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000485
486 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000487 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
488 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000489 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000490
Chris Lattner74734132002-08-17 22:01:27 +0000491 TheModule = Mod = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000492
Chris Lattner00950542001-06-06 20:29:01 +0000493 while (Buf < EndBuf) {
494 const uchar *OldBuf = Buf;
Chris Lattner74734132002-08-17 22:01:27 +0000495 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
Chris Lattner00950542001-06-06 20:29:01 +0000496 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000497 case BytecodeFormat::ConstantPool:
498 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
499 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000500 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000501 }
502 break;
503
Chris Lattner1d670cc2001-09-07 16:37:43 +0000504 case BytecodeFormat::ModuleGlobalInfo:
505 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
506
Chris Lattner74734132002-08-17 22:01:27 +0000507 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
508 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000509 }
510 break;
511
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000512 case BytecodeFormat::Function: {
513 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000514 if (ParseMethod(Buf, Buf+Size, Mod)) {
515 delete Mod; return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000516 }
517 break;
518 }
519
520 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000521 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000522 if (ParseSymbolTable(Buf, Buf+Size, Mod->getSymbolTableSure())) {
523 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000524 }
525 break;
526
527 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000528 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000529 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000530 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000531 break;
532 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000533 BCR_TRACE(1, "} end block\n");
Chris Lattner74734132002-08-17 22:01:27 +0000534 if (align32(Buf, EndBuf)) { delete Mod; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000535 }
536
Chris Lattner74734132002-08-17 22:01:27 +0000537 if (!FunctionSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000538 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000539 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000540 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000541
542 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000543 return false;
544}
545
546Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
547 LateResolveValues.clear();
548 unsigned Sig;
549 // Read and check signature...
550 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000551 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
552 Error = "Invalid bytecode signature!";
Chris Lattner74734132002-08-17 22:01:27 +0000553 return 0; // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000554 }
Chris Lattner00950542001-06-06 20:29:01 +0000555
556 Module *Result;
557 if (ParseModule(Buf, EndBuf, Result)) return 0;
558 return Result;
559}
560
561
Anand Shukla4f2d5a42002-09-20 20:57:54 +0000562Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) {
Chris Lattner00950542001-06-06 20:29:01 +0000563 BytecodeParser Parser;
564 return Parser.ParseBytecode(Buffer, Buffer+Length);
565}
566
567// Parse and return a class file...
568//
Chris Lattner697954c2002-01-20 22:54:45 +0000569Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000570 struct stat StatBuf;
571 Module *Result = 0;
572
Chris Lattner697954c2002-01-20 22:54:45 +0000573 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000574 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000575 if (FD == -1) {
576 if (ErrorStr) *ErrorStr = "Error opening file!";
Chris Lattner74734132002-08-17 22:01:27 +0000577 return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000578 }
Chris Lattner00950542001-06-06 20:29:01 +0000579
Chris Lattner74734132002-08-17 22:01:27 +0000580 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000581
582 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000583 if (Length == 0) {
584 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
Chris Lattner74734132002-08-17 22:01:27 +0000585 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000586 }
Chris Lattner00950542001-06-06 20:29:01 +0000587 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
588 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000589 if (Buffer == (uchar*)-1) {
590 if (ErrorStr) *ErrorStr = "Error mmapping file!";
Chris Lattner74734132002-08-17 22:01:27 +0000591 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000592 }
Chris Lattner00950542001-06-06 20:29:01 +0000593
594 BytecodeParser Parser;
595 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
596
597 munmap((char*)Buffer, Length);
598 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000599 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000600 } else { // Read from stdin
601 size_t FileSize = 0;
602 int BlockSize;
603 uchar Buffer[4096], *FileData = 0;
604 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner74734132002-08-17 22:01:27 +0000605 if (BlockSize == -1) { free(FileData); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000606
607 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
608 memcpy(FileData+FileSize, Buffer, BlockSize);
609 FileSize += BlockSize;
610 }
611
Chris Lattnerd6b65252001-10-24 01:15:12 +0000612 if (FileSize == 0) {
613 if (ErrorStr) *ErrorStr = "Standard Input empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000614 free(FileData); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000615 }
Chris Lattner00950542001-06-06 20:29:01 +0000616
Chris Lattner915ce8a2002-08-18 00:38:32 +0000617#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000618#if ALIGN_PTRS
619 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
620 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
621 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000622 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000623 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000624#else
625 uchar *Buf = FileData;
626#endif
627
628 BytecodeParser Parser;
629 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
630
631#if ALIGN_PTRS
632 munmap((char*)Buf, FileSize); // Free mmap'd data area
633#else
634 free(FileData); // Free realloc'd block of memory
635#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000636
637 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000638 }
639
640 return Result;
641}