blob: f84ad565cad2fabe5ffab75a9d606350d19dda85 [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//
8// TODO: Make error message outputs be configurable depending on an option?
9// 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"
Chris Lattner70cc3392001-09-10 07:58:01 +000016#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000022#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <fcntl.h>
25#include <unistd.h>
26#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000027#include <iostream>
28using std::cerr;
29using std::make_pair;
Chris Lattner00950542001-06-06 20:29:01 +000030
31bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
32 if (Ty->isPrimitiveType()) {
33 Slot = Ty->getPrimitiveID();
34 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000035 // Check the method level types first...
36 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
37 MethodTypeValues.end(), Ty);
38 if (I != MethodTypeValues.end()) {
39 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
40 (&*I - &MethodTypeValues[0]);
41 } else {
42 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
43 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
44 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
45 }
Chris Lattner00950542001-06-06 20:29:01 +000046 }
Chris Lattner697954c2002-01-20 22:54:45 +000047 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000048 return false;
49}
50
51const Type *BytecodeParser::getType(unsigned ID) {
52 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
53 if (T) return T;
54
Chris Lattner697954c2002-01-20 22:54:45 +000055 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000056
57 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +000058 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattnercfe26c92001-10-01 18:26:53 +000060 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000061}
62
Chris Lattner697954c2002-01-20 22:54:45 +000063int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000064 unsigned type;
Chris Lattner05950c32001-10-13 06:47:01 +000065 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
Chris Lattner1d670cc2001-09-07 16:37:43 +000066 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000067
68 if (ValueTab.size() <= type)
69 ValueTab.resize(type+1, ValueList());
70
71 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000072 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000073 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattner05950c32001-10-13 06:47:01 +000075 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000076}
77
78Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
79 unsigned Num = oNum;
80 unsigned type; // The type plane it lives in...
81
Chris Lattner3d3f2892001-07-28 17:50:18 +000082 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000083
84 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000086 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
87 if (T) return (Value*)T; // Asked for a primitive type...
88
89 // Otherwise, derived types need offset...
90 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000091
92 // Is it a module level type?
93 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000094 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000095
96 // Nope, is it a method level type?
97 Num -= ModuleTypeValues.size();
98 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000099 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000100
101 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104 if (type < ModuleValues.size()) {
105 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000106 return ModuleValues[type][Num];
107 Num -= ModuleValues[type].size();
108 }
109
110 if (Values.size() > type && Values[type].size() > Num)
111 return Values[type][Num];
112
Chris Lattner3d3f2892001-07-28 17:50:18 +0000113 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000114
115 Value *d = 0;
116 switch (Ty->getPrimitiveID()) {
117 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000118 case Type::FunctionTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000119 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000120 << Ty->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000121 d = new MethPHolder(Ty, oNum);
Chris Lattner05950c32001-10-13 06:47:01 +0000122 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000123 return d;
124 default: d = new DefPHolder(Ty, oNum); break;
125 }
126
127 assert(d != 0 && "How did we not make something?");
Chris Lattner05950c32001-10-13 06:47:01 +0000128 if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000129 return d;
130}
131
132bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
133 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000134 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000135 ValueList &DL = ValTab[ty];
136 unsigned Size;
137 while ((Size = DL.size())) {
138 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
139
140 Value *D = DL[Size-1];
141 DL.pop_back();
142
143 Value *NewDef = getValue(D->getType(), IDNumber, false);
144 if (NewDef == 0) {
145 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000146 cerr << "Unresolvable reference found: <"
147 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000148 } else {
149 // Fixup all of the uses of this placeholder def...
150 D->replaceAllUsesWith(NewDef);
151
152 // Now that all the uses are gone, delete the placeholder...
153 // If we couldn't find a def (error case), then leak a little
154 delete D; // memory, 'cause otherwise we can't remove all uses!
155 }
156 }
157 }
158
159 return Error;
160}
161
162bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
163 BasicBlock *&BB) {
164 BB = new BasicBlock();
165
166 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000167 Instruction *Inst;
168 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000169 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000170 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000171 }
172
Chris Lattner1d670cc2001-09-07 16:37:43 +0000173 if (Inst == 0) { delete BB; return failure(true); }
Chris Lattner05950c32001-10-13 06:47:01 +0000174 if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000175
Chris Lattner1d670cc2001-09-07 16:37:43 +0000176 BB->getInstList().push_back(Inst);
177
178 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000179 }
180
181 return false;
182}
183
Chris Lattner1d670cc2001-09-07 16:37:43 +0000184bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
185 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 while (Buf < EndBuf) {
187 // Symtab block header: [num entries][type id number]
188 unsigned NumEntries, Typ;
189 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000190 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000191 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000192 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000193
Chris Lattner1d670cc2001-09-07 16:37:43 +0000194 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
195 " entries\n");
196
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000197 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 // Symtab entry: [def slot #][name]
199 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000200 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner697954c2002-01-20 22:54:45 +0000201 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000202 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000203 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000204
205 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000206 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000207 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000208 return failure(true);
209 }
210 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000211 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000212
213 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000214 }
215 }
216
Chris Lattner3d3f2892001-07-28 17:50:18 +0000217 if (Buf > EndBuf) return failure(true);
218 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000219}
220
Chris Lattner05950c32001-10-13 06:47:01 +0000221// DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000222// form of ConstantPointerRef.
Chris Lattner05950c32001-10-13 06:47:01 +0000223//
224void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
225 // Check to see if there is a forward reference to this global variable...
226 // if there is, eliminate it and patch the reference to use the new def'n.
227 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
228
229 if (I != GlobalRefs.end()) {
230 GlobalVariable *OldGV = I->second; // Get the placeholder...
231 BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
232
233 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000234 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner05950c32001-10-13 06:47:01 +0000235 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
236 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000237 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
238 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner05950c32001-10-13 06:47:01 +0000239 assert(CPPR->getValue() == OldGV && "Something isn't happy");
240
241 BCR_TRACE(4, "Mutating Forward Ref!\n");
242
243 // Change the const pool reference to point to the real global variable
244 // now. This should drop a use from the OldGV.
245 CPPR->mutateReference(GV);
246 }
247
248 // Remove GV from the module...
249 GV->getParent()->getGlobalList().remove(OldGV);
250 delete OldGV; // Delete the old placeholder
251
252 // Remove the map entry for the global now that it has been created...
253 GlobalRefs.erase(I);
254 }
255}
Chris Lattner00950542001-06-06 20:29:01 +0000256
257bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
258 Module *C) {
259 // Clear out the local values table...
260 Values.clear();
Chris Lattnerd6b65252001-10-24 01:15:12 +0000261 if (MethodSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000262 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000263 return failure(true); // Unexpected method!
264 }
Chris Lattner00950542001-06-06 20:29:01 +0000265
Chris Lattneref9c23f2001-10-03 14:53:21 +0000266 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000267 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattneref9c23f2001-10-03 14:53:21 +0000268 if (MTy == 0) return failure(true); // Not ptr to method!
269
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000270 unsigned isInternal;
271 if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
272
Chris Lattner00950542001-06-06 20:29:01 +0000273 unsigned MethSlot = MethodSignatureList.front().second;
274 MethodSignatureList.pop_front();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000275 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000276
Chris Lattner697954c2002-01-20 22:54:45 +0000277 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000278
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000279 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
280 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000281 It != Params.end(); ++It) {
Chris Lattner73e21422002-04-09 19:48:49 +0000282 Argument *FA = new Argument(*It);
Chris Lattner79df7c02002-03-26 18:01:55 +0000283 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000284 Error = "Error reading method arguments!\n";
285 delete M; return failure(true);
286 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000287 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000288 }
289
290 while (Buf < EndBuf) {
291 unsigned Type, Size;
292 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000293 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000294 Error = "Error reading Function level block!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000295 delete M; return failure(true);
296 }
Chris Lattner00950542001-06-06 20:29:01 +0000297
298 switch (Type) {
299 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
301 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000302 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000303 }
304 break;
305
306 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000307 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000308 BasicBlock *BB;
309 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000310 insertValue(BB, Values) == -1) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000311 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000312 }
313
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000314 M->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000315 break;
316 }
317
318 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000319 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
320 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000321 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000322 }
323 break;
324
325 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000326 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000327 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000328 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000329 break;
330 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000331 BCR_TRACE(2, "} end block\n");
332
Chris Lattner00950542001-06-06 20:29:01 +0000333 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000334 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000335 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000336 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000337 }
338 }
339
340 if (postResolveValues(LateResolveValues) ||
341 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000342 Error = "Error resolving method values!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000343 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000344 }
345
Chris Lattneref9c23f2001-10-03 14:53:21 +0000346 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000347 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000348 assert(isa<Function>(MethPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000349
350 unsigned type; // Type slot
351 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000352 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000353
Chris Lattner79df7c02002-03-26 18:01:55 +0000354 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000355
356 // Replace placeholder with the real method pointer...
357 ModuleValues[type][MethSlot] = M;
358
Chris Lattnere4d71a12001-09-14 22:03:42 +0000359 // Clear out method level types...
360 MethodTypeValues.clear();
361
Chris Lattner00950542001-06-06 20:29:01 +0000362 // If anyone is using the placeholder make them use the real method instead
363 MethPHolder->replaceAllUsesWith(M);
364
365 // We don't need the placeholder anymore!
366 delete MethPHolder;
367
Chris Lattnerb847f512001-10-14 23:28:41 +0000368 // If the method is empty, we don't need the method argument entries...
369 if (M->isExternal())
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000370 M->getArgumentList().clear();
Chris Lattnerb847f512001-10-14 23:28:41 +0000371
Chris Lattner05950c32001-10-13 06:47:01 +0000372 DeclareNewGlobalValue(M, MethSlot);
373
Chris Lattner00950542001-06-06 20:29:01 +0000374 return false;
375}
376
377bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000378 Module *Mod) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000379 if (!MethodSignatureList.empty()) {
380 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000381 return failure(true); // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000382 }
Chris Lattner00950542001-06-06 20:29:01 +0000383
Chris Lattner70cc3392001-09-10 07:58:01 +0000384 // Read global variables...
385 unsigned VarType;
386 if (read_vbr(Buf, End, VarType)) return failure(true);
387 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000388 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
389 // bit2 = isInternal, bit3+ = slot#
390 const Type *Ty = getType(VarType >> 3);
Chris Lattner9b625032002-05-06 16:15:30 +0000391 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000392 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner70cc3392001-09-10 07:58:01 +0000393 return failure(true);
394 }
395
Chris Lattneref9c23f2001-10-03 14:53:21 +0000396 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000397 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000398
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000399 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000400 if (VarType & 2) { // Does it have an initalizer?
401 // Do not improvise... values must have been stored in the constant pool,
402 // which should have been read before now.
403 //
404 unsigned InitSlot;
405 if (read_vbr(Buf, End, InitSlot)) return failure(true);
406
Chris Lattner05950c32001-10-13 06:47:01 +0000407 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000408 if (V == 0) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000409 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000410 }
411
Chris Lattner70cc3392001-09-10 07:58:01 +0000412 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000413 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
414 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000415 int DestSlot = insertValue(GV, ModuleValues);
416 if (DestSlot == -1) return failure(true);
417
418 Mod->getGlobalList().push_back(GV);
419
420 DeclareNewGlobalValue(GV, unsigned(DestSlot));
421
422 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000423 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000424
425 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattner70cc3392001-09-10 07:58:01 +0000426 }
427
Chris Lattner00950542001-06-06 20:29:01 +0000428 // Read the method signatures for all of the methods that are coming, and
429 // create fillers in the Value tables.
430 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000431 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000432 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
433 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000434 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000435 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
436 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000437 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000438 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000439
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000440 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000441 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000442
Chris Lattner1d670cc2001-09-07 16:37:43 +0000443 // When the ModuleGlobalInfo section is read, we load the type of each
444 // method and the 'ModuleValues' slot that it lands in. We then load a
445 // placeholder into its slot to reserve it. When the method is loaded, this
446 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000447
448 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000449 Value *Val = new MethPHolder(Ty, 0);
Chris Lattner05950c32001-10-13 06:47:01 +0000450 if (insertValue(Val, ModuleValues) == -1) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000451
452 // Figure out which entry of its typeslot it went into...
453 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000454 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000455
456 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
457
458 // Keep track of this information in a linked list that is emptied as
459 // methods are loaded...
460 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000461 MethodSignatureList.push_back(
462 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000463 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000464 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000465 }
466
Chris Lattner3d3f2892001-07-28 17:50:18 +0000467 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000468
469 // This is for future proofing... in the future extra fields may be added that
470 // we don't understand, so we transparently ignore them.
471 //
472 Buf = End;
473 return false;
474}
475
476bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
477 Module *&C) {
478
479 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000480 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000481 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
482 Error = "Expected Module packet!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000483 return failure(true); // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000484 }
Chris Lattner00950542001-06-06 20:29:01 +0000485
Chris Lattner1d670cc2001-09-07 16:37:43 +0000486 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000487 MethodSignatureList.clear(); // Just in case...
488
489 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000490 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
491 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000492 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000493
Chris Lattner05950c32001-10-13 06:47:01 +0000494 TheModule = C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000495 while (Buf < EndBuf) {
496 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000497 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000498 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000499 case BytecodeFormat::ConstantPool:
500 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
501 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000502 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000503 }
504 break;
505
Chris Lattner1d670cc2001-09-07 16:37:43 +0000506 case BytecodeFormat::ModuleGlobalInfo:
507 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
508
509 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000510 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000511 }
512 break;
513
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000514 case BytecodeFormat::Function: {
515 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000516 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000517 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000518 }
519 break;
520 }
521
522 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000523 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
524 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000525 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000526 }
527 break;
528
529 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000530 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000531 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000532 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000533 break;
534 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000535 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000536 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000537 }
538
Chris Lattnerd6b65252001-10-24 01:15:12 +0000539 if (!MethodSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000540 Error = "Function expected, but bytecode stream at end!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000541 return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000542 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000543
544 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000545 return false;
546}
547
548Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
549 LateResolveValues.clear();
550 unsigned Sig;
551 // Read and check signature...
552 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000553 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
554 Error = "Invalid bytecode signature!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000555 return failure<Module*>(0); // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000556 }
Chris Lattner00950542001-06-06 20:29:01 +0000557
558 Module *Result;
559 if (ParseModule(Buf, EndBuf, Result)) return 0;
560 return Result;
561}
562
563
564Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
565 BytecodeParser Parser;
566 return Parser.ParseBytecode(Buffer, Buffer+Length);
567}
568
569// Parse and return a class file...
570//
Chris Lattner697954c2002-01-20 22:54:45 +0000571Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000572 struct stat StatBuf;
573 Module *Result = 0;
574
Chris Lattner697954c2002-01-20 22:54:45 +0000575 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000576 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000577 if (FD == -1) {
578 if (ErrorStr) *ErrorStr = "Error opening file!";
579 return failure<Module*>(0);
580 }
Chris Lattner00950542001-06-06 20:29:01 +0000581
Chris Lattner3d3f2892001-07-28 17:50:18 +0000582 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000583
584 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000585 if (Length == 0) {
586 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
587 close(FD); return failure<Module*>(0);
588 }
Chris Lattner00950542001-06-06 20:29:01 +0000589 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
590 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000591 if (Buffer == (uchar*)-1) {
592 if (ErrorStr) *ErrorStr = "Error mmapping file!";
593 close(FD); return failure<Module*>(0);
594 }
Chris Lattner00950542001-06-06 20:29:01 +0000595
596 BytecodeParser Parser;
597 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
598
599 munmap((char*)Buffer, Length);
600 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000601 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000602 } else { // Read from stdin
603 size_t FileSize = 0;
604 int BlockSize;
605 uchar Buffer[4096], *FileData = 0;
606 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000607 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000608
609 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
610 memcpy(FileData+FileSize, Buffer, BlockSize);
611 FileSize += BlockSize;
612 }
613
Chris Lattnerd6b65252001-10-24 01:15:12 +0000614 if (FileSize == 0) {
615 if (ErrorStr) *ErrorStr = "Standard Input empty!";
616 free(FileData); return failure<Module*>(0);
617 }
Chris Lattner00950542001-06-06 20:29:01 +0000618
619#define ALIGN_PTRS 1
620#if ALIGN_PTRS
621 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
622 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
623 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000624 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000625 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000626#else
627 uchar *Buf = FileData;
628#endif
629
630 BytecodeParser Parser;
631 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
632
633#if ALIGN_PTRS
634 munmap((char*)Buf, FileSize); // Free mmap'd data area
635#else
636 free(FileData); // Free realloc'd block of memory
637#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000638
639 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000640 }
641
642 return Result;
643}