blob: 0e406259769c5c306aab8905e78858f119b36568 [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;
171 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000172 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000173 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000174 }
175
Chris Lattner74734132002-08-17 22:01:27 +0000176 if (Inst == 0) { delete BB; return true; }
177 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000178
Chris Lattner1d670cc2001-09-07 16:37:43 +0000179 BB->getInstList().push_back(Inst);
180
181 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000182 }
183
184 return false;
185}
186
Chris Lattner1d670cc2001-09-07 16:37:43 +0000187bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
188 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000189 while (Buf < EndBuf) {
190 // Symtab block header: [num entries][type id number]
191 unsigned NumEntries, Typ;
192 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000193 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000194 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000195 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000196
Chris Lattner1d670cc2001-09-07 16:37:43 +0000197 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
198 " entries\n");
199
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000200 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000201 // Symtab entry: [def slot #][name]
202 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000203 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000204 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000205 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000206 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000207
208 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000209 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000210 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000211 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000212 }
213 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000214 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000215
216 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000217 }
218 }
219
Chris Lattner74734132002-08-17 22:01:27 +0000220 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000221 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000222}
223
Chris Lattner74734132002-08-17 22:01:27 +0000224void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
225 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(NewV->getType(),Slot));
226 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000227
Chris Lattner74734132002-08-17 22:01:27 +0000228 BCR_TRACE(3, "Mutating forward refs!\n");
229 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000230
Chris Lattner74734132002-08-17 22:01:27 +0000231 // Loop over all of the uses of the Value. What they are depends
232 // on what NewV is. Replacing a use of the old reference takes the
233 // use off the use list, so loop with !use_empty(), not the use_iterator.
234 while (!VPH->use_empty()) {
235 Constant *C = cast<Constant>(VPH->use_back());
236 unsigned numReplaced = C->mutateReferences(VPH, NewV);
237 assert(numReplaced > 0 && "Supposed user wasn't really a user?");
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000238
Chris Lattner74734132002-08-17 22:01:27 +0000239 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) {
240 // Remove the placeholder GlobalValue from the module...
241 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000242 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000243 }
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000244
Chris Lattner74734132002-08-17 22:01:27 +0000245 delete VPH; // Delete the old placeholder
246 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000247}
248
Chris Lattner00950542001-06-06 20:29:01 +0000249bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
250 Module *C) {
251 // Clear out the local values table...
252 Values.clear();
Chris Lattner74734132002-08-17 22:01:27 +0000253 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000254 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000255 return true; // Unexpected method!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000256 }
Chris Lattner00950542001-06-06 20:29:01 +0000257
Chris Lattner74734132002-08-17 22:01:27 +0000258 const PointerType *PMTy = FunctionSignatureList.back().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000259 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000260 if (MTy == 0) return true; // Not ptr to method!
Chris Lattneref9c23f2001-10-03 14:53:21 +0000261
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000262 unsigned isInternal;
Chris Lattner74734132002-08-17 22:01:27 +0000263 if (read_vbr(Buf, EndBuf, isInternal)) return true;
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000264
Chris Lattner74734132002-08-17 22:01:27 +0000265 unsigned MethSlot = FunctionSignatureList.back().second;
266 FunctionSignatureList.pop_back();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000267 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000268
Chris Lattner697954c2002-01-20 22:54:45 +0000269 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000270
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000271 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
272 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000273 It != Params.end(); ++It) {
Chris Lattner73e21422002-04-09 19:48:49 +0000274 Argument *FA = new Argument(*It);
Chris Lattner79df7c02002-03-26 18:01:55 +0000275 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000276 Error = "Error reading method arguments!\n";
Chris Lattner74734132002-08-17 22:01:27 +0000277 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000278 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000279 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000280 }
281
282 while (Buf < EndBuf) {
283 unsigned Type, Size;
284 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000285 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000286 Error = "Error reading Function level block!";
Chris Lattner74734132002-08-17 22:01:27 +0000287 delete M; return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000288 }
Chris Lattner00950542001-06-06 20:29:01 +0000289
290 switch (Type) {
291 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000292 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
293 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000294 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000295 }
296 break;
297
298 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000299 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000300 BasicBlock *BB;
301 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000302 insertValue(BB, Values) == -1) {
Chris Lattner74734132002-08-17 22:01:27 +0000303 delete M; return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
305
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000306 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000307 break;
308 }
309
310 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000311 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
312 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner74734132002-08-17 22:01:27 +0000313 delete M; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
315 break;
316
317 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000318 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000319 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000320 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000321 break;
322 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000323 BCR_TRACE(2, "} end block\n");
324
Chris Lattner00950542001-06-06 20:29:01 +0000325 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000326 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000327 delete M; // Malformed bc file, read past end of block.
Chris Lattner74734132002-08-17 22:01:27 +0000328 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000329 }
330 }
331
332 if (postResolveValues(LateResolveValues) ||
333 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000334 Error = "Error resolving method values!";
Chris Lattner74734132002-08-17 22:01:27 +0000335 delete M; return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000336 }
337
Chris Lattner74734132002-08-17 22:01:27 +0000338 Value *FunctionPHolder = getValue(PMTy, MethSlot, false);
339 assert(FunctionPHolder && "Something is broken no placeholder found!");
340 assert(isa<Function>(FunctionPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000341
342 unsigned type; // Type slot
343 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000344 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000345
Chris Lattner79df7c02002-03-26 18:01:55 +0000346 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000347
348 // Replace placeholder with the real method pointer...
349 ModuleValues[type][MethSlot] = M;
350
Chris Lattnere4d71a12001-09-14 22:03:42 +0000351 // Clear out method level types...
352 MethodTypeValues.clear();
353
Chris Lattner00950542001-06-06 20:29:01 +0000354 // If anyone is using the placeholder make them use the real method instead
Chris Lattner74734132002-08-17 22:01:27 +0000355 FunctionPHolder->replaceAllUsesWith(M);
Chris Lattner00950542001-06-06 20:29:01 +0000356
357 // We don't need the placeholder anymore!
Chris Lattner74734132002-08-17 22:01:27 +0000358 delete FunctionPHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000359
Chris Lattnerb847f512001-10-14 23:28:41 +0000360 // If the method is empty, we don't need the method argument entries...
361 if (M->isExternal())
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000362 M->getArgumentList().clear();
Chris Lattnerb847f512001-10-14 23:28:41 +0000363
Chris Lattner74734132002-08-17 22:01:27 +0000364 ResolveReferencesToValue(M, MethSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000365
Chris Lattner00950542001-06-06 20:29:01 +0000366 return false;
367}
368
369bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000370 Module *Mod) {
Chris Lattner74734132002-08-17 22:01:27 +0000371 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000372 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000373 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000374 }
Chris Lattner00950542001-06-06 20:29:01 +0000375
Chris Lattner70cc3392001-09-10 07:58:01 +0000376 // Read global variables...
377 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000378 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000379 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000380 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
381 // bit2 = isInternal, bit3+ = slot#
382 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000383 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000384 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000385 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000386 }
387
Chris Lattneref9c23f2001-10-03 14:53:21 +0000388 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000389 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000390
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000391 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000392 if (VarType & 2) { // Does it have an initalizer?
393 // Do not improvise... values must have been stored in the constant pool,
394 // which should have been read before now.
395 //
396 unsigned InitSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000397 if (read_vbr(Buf, End, InitSlot)) return true;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000398
Chris Lattner05950c32001-10-13 06:47:01 +0000399 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000400 if (V == 0) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000401 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000402 }
403
Chris Lattner70cc3392001-09-10 07:58:01 +0000404 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000405 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
406 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000407 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000408 if (DestSlot == -1) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000409
410 Mod->getGlobalList().push_back(GV);
411
Chris Lattner74734132002-08-17 22:01:27 +0000412 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000413
414 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000415 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000416
Chris Lattner74734132002-08-17 22:01:27 +0000417 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000418 }
419
Chris Lattner00950542001-06-06 20:29:01 +0000420 // Read the method signatures for all of the methods that are coming, and
421 // create fillers in the Value tables.
Chris Lattner74734132002-08-17 22:01:27 +0000422 unsigned FnSignature;
423 if (read_vbr(Buf, End, FnSignature)) return true;
424 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
425 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000426 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000427 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
428 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000429 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000430 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000431
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000432 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000433 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000434
Chris Lattner1d670cc2001-09-07 16:37:43 +0000435 // When the ModuleGlobalInfo section is read, we load the type of each
436 // method and the 'ModuleValues' slot that it lands in. We then load a
437 // placeholder into its slot to reserve it. When the method is loaded, this
438 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000439
440 // Insert the placeholder...
Chris Lattner74734132002-08-17 22:01:27 +0000441 Value *Val = new FunctionPHolder(Ty, 0);
442 if (insertValue(Val, ModuleValues) == -1) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000443
444 // Figure out which entry of its typeslot it went into...
445 unsigned TypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000446 if (getTypeSlot(Val->getType(), TypeSlot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000447
448 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
449
450 // Keep track of this information in a linked list that is emptied as
451 // methods are loaded...
452 //
Chris Lattner74734132002-08-17 22:01:27 +0000453 FunctionSignatureList.push_back(
Chris Lattneref9c23f2001-10-03 14:53:21 +0000454 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner74734132002-08-17 22:01:27 +0000455 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000456 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000457 }
458
Chris Lattner74734132002-08-17 22:01:27 +0000459 if (align32(Buf, End)) return true;
460
461 // Now that the function signature list is set up, reverse it so that we can
462 // remove elements efficiently from the back of the vector.
463 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000464
465 // This is for future proofing... in the future extra fields may be added that
466 // we don't understand, so we transparently ignore them.
467 //
468 Buf = End;
469 return false;
470}
471
472bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
Chris Lattner74734132002-08-17 22:01:27 +0000473 Module *&Mod) {
Chris Lattner00950542001-06-06 20:29:01 +0000474
475 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000476 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000477 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
478 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000479 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000480 }
Chris Lattner00950542001-06-06 20:29:01 +0000481
Chris Lattner1d670cc2001-09-07 16:37:43 +0000482 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000483 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000484
485 // Read into instance variables...
Chris Lattner74734132002-08-17 22:01:27 +0000486 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
487 if (align32(Buf, EndBuf)) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000488 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000489
Chris Lattner74734132002-08-17 22:01:27 +0000490 TheModule = Mod = new Module();
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000491
Chris Lattner00950542001-06-06 20:29:01 +0000492 while (Buf < EndBuf) {
493 const uchar *OldBuf = Buf;
Chris Lattner74734132002-08-17 22:01:27 +0000494 if (readBlock(Buf, EndBuf, Type, Size)) { delete Mod; return true;}
Chris Lattner00950542001-06-06 20:29:01 +0000495 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000496 case BytecodeFormat::ConstantPool:
497 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
498 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner74734132002-08-17 22:01:27 +0000499 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000500 }
501 break;
502
Chris Lattner1d670cc2001-09-07 16:37:43 +0000503 case BytecodeFormat::ModuleGlobalInfo:
504 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
505
Chris Lattner74734132002-08-17 22:01:27 +0000506 if (ParseModuleGlobalInfo(Buf, Buf+Size, Mod)) {
507 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000508 }
509 break;
510
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000511 case BytecodeFormat::Function: {
512 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000513 if (ParseMethod(Buf, Buf+Size, Mod)) {
514 delete Mod; return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000515 }
516 break;
517 }
518
519 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000520 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000521 if (ParseSymbolTable(Buf, Buf+Size, Mod->getSymbolTableSure())) {
522 delete Mod; return true;
Chris Lattner00950542001-06-06 20:29:01 +0000523 }
524 break;
525
526 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000527 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000528 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000529 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000530 break;
531 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000532 BCR_TRACE(1, "} end block\n");
Chris Lattner74734132002-08-17 22:01:27 +0000533 if (align32(Buf, EndBuf)) { delete Mod; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000534 }
535
Chris Lattner74734132002-08-17 22:01:27 +0000536 if (!FunctionSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000537 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000538 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000539 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000540
541 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000542 return false;
543}
544
545Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
546 LateResolveValues.clear();
547 unsigned Sig;
548 // Read and check signature...
549 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000550 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
551 Error = "Invalid bytecode signature!";
Chris Lattner74734132002-08-17 22:01:27 +0000552 return 0; // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000553 }
Chris Lattner00950542001-06-06 20:29:01 +0000554
555 Module *Result;
556 if (ParseModule(Buf, EndBuf, Result)) return 0;
557 return Result;
558}
559
560
561Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
562 BytecodeParser Parser;
563 return Parser.ParseBytecode(Buffer, Buffer+Length);
564}
565
566// Parse and return a class file...
567//
Chris Lattner697954c2002-01-20 22:54:45 +0000568Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000569 struct stat StatBuf;
570 Module *Result = 0;
571
Chris Lattner697954c2002-01-20 22:54:45 +0000572 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000573 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000574 if (FD == -1) {
575 if (ErrorStr) *ErrorStr = "Error opening file!";
Chris Lattner74734132002-08-17 22:01:27 +0000576 return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000577 }
Chris Lattner00950542001-06-06 20:29:01 +0000578
Chris Lattner74734132002-08-17 22:01:27 +0000579 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000580
581 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000582 if (Length == 0) {
583 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
Chris Lattner74734132002-08-17 22:01:27 +0000584 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000585 }
Chris Lattner00950542001-06-06 20:29:01 +0000586 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
587 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000588 if (Buffer == (uchar*)-1) {
589 if (ErrorStr) *ErrorStr = "Error mmapping file!";
Chris Lattner74734132002-08-17 22:01:27 +0000590 close(FD); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000591 }
Chris Lattner00950542001-06-06 20:29:01 +0000592
593 BytecodeParser Parser;
594 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
595
596 munmap((char*)Buffer, Length);
597 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000598 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000599 } else { // Read from stdin
600 size_t FileSize = 0;
601 int BlockSize;
602 uchar Buffer[4096], *FileData = 0;
603 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner74734132002-08-17 22:01:27 +0000604 if (BlockSize == -1) { free(FileData); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000605
606 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
607 memcpy(FileData+FileSize, Buffer, BlockSize);
608 FileSize += BlockSize;
609 }
610
Chris Lattnerd6b65252001-10-24 01:15:12 +0000611 if (FileSize == 0) {
612 if (ErrorStr) *ErrorStr = "Standard Input empty!";
Chris Lattner74734132002-08-17 22:01:27 +0000613 free(FileData); return 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000614 }
Chris Lattner00950542001-06-06 20:29:01 +0000615
616#define ALIGN_PTRS 1
617#if ALIGN_PTRS
618 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
619 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
620 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000621 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000622 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000623#else
624 uchar *Buf = FileData;
625#endif
626
627 BytecodeParser Parser;
628 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
629
630#if ALIGN_PTRS
631 munmap((char*)Buf, FileSize); // Free mmap'd data area
632#else
633 free(FileData); // Free realloc'd block of memory
634#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000635
636 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000637 }
638
639 return Result;
640}