blob: fff2d02898c45002b4d98e6eebadf9d33e753437 [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"
18#include "llvm/BasicBlock.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000019#include "llvm/ConstantVals.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/iOther.h"
Chris Lattner73e21422002-04-09 19:48:49 +000022#include "llvm/Argument.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <fcntl.h>
27#include <unistd.h>
28#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000029#include <iostream>
30using std::cerr;
31using std::make_pair;
Chris Lattner00950542001-06-06 20:29:01 +000032
33bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
34 if (Ty->isPrimitiveType()) {
35 Slot = Ty->getPrimitiveID();
36 } else {
Chris Lattner1d670cc2001-09-07 16:37:43 +000037 // Check the method level types first...
38 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
39 MethodTypeValues.end(), Ty);
40 if (I != MethodTypeValues.end()) {
41 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
42 (&*I - &MethodTypeValues[0]);
43 } else {
44 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
45 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
46 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
47 }
Chris Lattner00950542001-06-06 20:29:01 +000048 }
Chris Lattner697954c2002-01-20 22:54:45 +000049 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000050 return false;
51}
52
53const Type *BytecodeParser::getType(unsigned ID) {
54 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
55 if (T) return T;
56
Chris Lattner697954c2002-01-20 22:54:45 +000057 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000058
59 const Value *D = getValue(Type::TypeTy, ID, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +000060 if (D == 0) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattnercfe26c92001-10-01 18:26:53 +000062 return cast<Type>(D);
Chris Lattner00950542001-06-06 20:29:01 +000063}
64
Chris Lattner697954c2002-01-20 22:54:45 +000065int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
Chris Lattner00950542001-06-06 20:29:01 +000066 unsigned type;
Chris Lattner05950c32001-10-13 06:47:01 +000067 if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
Chris Lattner1d670cc2001-09-07 16:37:43 +000068 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000069
70 if (ValueTab.size() <= type)
71 ValueTab.resize(type+1, ValueList());
72
73 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000074 // << "] = " << Val << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000075 ValueTab[type].push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000076
Chris Lattner05950c32001-10-13 06:47:01 +000077 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +000078}
79
80Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
81 unsigned Num = oNum;
82 unsigned type; // The type plane it lives in...
83
Chris Lattner3d3f2892001-07-28 17:50:18 +000084 if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
Chris Lattner00950542001-06-06 20:29:01 +000085
86 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +000087 assert(Create == false);
Chris Lattner00950542001-06-06 20:29:01 +000088 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
89 if (T) return (Value*)T; // Asked for a primitive type...
90
91 // Otherwise, derived types need offset...
92 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +000093
94 // Is it a module level type?
95 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +000096 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +000097
98 // Nope, is it a method level type?
99 Num -= ModuleTypeValues.size();
100 if (Num < MethodTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000101 return (Value*)MethodTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102
103 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000104 }
105
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 if (type < ModuleValues.size()) {
107 if (Num < ModuleValues[type].size())
Chris Lattner00950542001-06-06 20:29:01 +0000108 return ModuleValues[type][Num];
109 Num -= ModuleValues[type].size();
110 }
111
112 if (Values.size() > type && Values[type].size() > Num)
113 return Values[type][Num];
114
Chris Lattner3d3f2892001-07-28 17:50:18 +0000115 if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000116
117 Value *d = 0;
118 switch (Ty->getPrimitiveID()) {
119 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000120 case Type::FunctionTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000121 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
Chris Lattner697954c2002-01-20 22:54:45 +0000122 << Ty->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000123 d = new MethPHolder(Ty, oNum);
Chris Lattner05950c32001-10-13 06:47:01 +0000124 if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000125 return d;
126 default: d = new DefPHolder(Ty, oNum); break;
127 }
128
129 assert(d != 0 && "How did we not make something?");
Chris Lattner05950c32001-10-13 06:47:01 +0000130 if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
Chris Lattner00950542001-06-06 20:29:01 +0000131 return d;
132}
133
134bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
135 bool Error = false;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000136 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
Chris Lattner00950542001-06-06 20:29:01 +0000137 ValueList &DL = ValTab[ty];
138 unsigned Size;
139 while ((Size = DL.size())) {
140 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
141
142 Value *D = DL[Size-1];
143 DL.pop_back();
144
145 Value *NewDef = getValue(D->getType(), IDNumber, false);
146 if (NewDef == 0) {
147 Error = true; // Unresolved thinger
Chris Lattner05950c32001-10-13 06:47:01 +0000148 cerr << "Unresolvable reference found: <"
149 << D->getType()->getDescription() << ">:" << IDNumber << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000150 } else {
151 // Fixup all of the uses of this placeholder def...
152 D->replaceAllUsesWith(NewDef);
153
154 // Now that all the uses are gone, delete the placeholder...
155 // If we couldn't find a def (error case), then leak a little
156 delete D; // memory, 'cause otherwise we can't remove all uses!
157 }
158 }
159 }
160
161 return Error;
162}
163
164bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
165 BasicBlock *&BB) {
166 BB = new BasicBlock();
167
168 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000169 Instruction *Inst;
170 if (ParseInstruction(Buf, EndBuf, Inst)) {
Chris Lattner00950542001-06-06 20:29:01 +0000171 delete BB;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000172 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000173 }
174
Chris Lattner1d670cc2001-09-07 16:37:43 +0000175 if (Inst == 0) { delete BB; return failure(true); }
Chris Lattner05950c32001-10-13 06:47:01 +0000176 if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000177
Chris Lattner1d670cc2001-09-07 16:37:43 +0000178 BB->getInstList().push_back(Inst);
179
180 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000181 }
182
183 return false;
184}
185
Chris Lattner1d670cc2001-09-07 16:37:43 +0000186bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
187 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000188 while (Buf < EndBuf) {
189 // Symtab block header: [num entries][type id number]
190 unsigned NumEntries, Typ;
191 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000192 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000193 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000194 if (Ty == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner1d670cc2001-09-07 16:37:43 +0000196 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
197 " entries\n");
198
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000199 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000200 // Symtab entry: [def slot #][name]
201 unsigned slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000202 if (read_vbr(Buf, EndBuf, slot)) return failure(true);
Chris Lattner697954c2002-01-20 22:54:45 +0000203 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000204 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000205 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000206
207 Value *D = getValue(Ty, slot, false); // Find mapping...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000208 if (D == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000209 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000210 return failure(true);
211 }
212 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
Chris Lattner697954c2002-01-20 22:54:45 +0000213 if (!isa<Instruction>(D)) cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000214
215 D->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000216 }
217 }
218
Chris Lattner3d3f2892001-07-28 17:50:18 +0000219 if (Buf > EndBuf) return failure(true);
220 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000221}
222
Chris Lattner05950c32001-10-13 06:47:01 +0000223// DeclareNewGlobalValue - Patch up forward references to global values in the
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000224// form of ConstantPointerRef.
Chris Lattner05950c32001-10-13 06:47:01 +0000225//
226void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
227 // Check to see if there is a forward reference to this global variable...
228 // if there is, eliminate it and patch the reference to use the new def'n.
229 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
230
231 if (I != GlobalRefs.end()) {
232 GlobalVariable *OldGV = I->second; // Get the placeholder...
233 BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
234
235 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000236 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner05950c32001-10-13 06:47:01 +0000237 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
238 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000239 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
240 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner05950c32001-10-13 06:47:01 +0000241 assert(CPPR->getValue() == OldGV && "Something isn't happy");
242
243 BCR_TRACE(4, "Mutating Forward Ref!\n");
244
245 // Change the const pool reference to point to the real global variable
246 // now. This should drop a use from the OldGV.
247 CPPR->mutateReference(GV);
248 }
249
250 // Remove GV from the module...
251 GV->getParent()->getGlobalList().remove(OldGV);
252 delete OldGV; // Delete the old placeholder
253
254 // Remove the map entry for the global now that it has been created...
255 GlobalRefs.erase(I);
256 }
257}
Chris Lattner00950542001-06-06 20:29:01 +0000258
259bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
260 Module *C) {
261 // Clear out the local values table...
262 Values.clear();
Chris Lattnerd6b65252001-10-24 01:15:12 +0000263 if (MethodSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000264 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000265 return failure(true); // Unexpected method!
266 }
Chris Lattner00950542001-06-06 20:29:01 +0000267
Chris Lattneref9c23f2001-10-03 14:53:21 +0000268 const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000269 const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
Chris Lattneref9c23f2001-10-03 14:53:21 +0000270 if (MTy == 0) return failure(true); // Not ptr to method!
271
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000272 unsigned isInternal;
273 if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
274
Chris Lattner00950542001-06-06 20:29:01 +0000275 unsigned MethSlot = MethodSignatureList.front().second;
276 MethodSignatureList.pop_front();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000277 Function *M = new Function(MTy, isInternal != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000278
Chris Lattner697954c2002-01-20 22:54:45 +0000279 BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000280
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000281 const FunctionType::ParamTypes &Params = MTy->getParamTypes();
282 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000283 It != Params.end(); ++It) {
Chris Lattner73e21422002-04-09 19:48:49 +0000284 Argument *FA = new Argument(*It);
Chris Lattner79df7c02002-03-26 18:01:55 +0000285 if (insertValue(FA, Values) == -1) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000286 Error = "Error reading method arguments!\n";
287 delete M; return failure(true);
288 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000289 M->getArgumentList().push_back(FA);
Chris Lattner00950542001-06-06 20:29:01 +0000290 }
291
292 while (Buf < EndBuf) {
293 unsigned Type, Size;
294 const uchar *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000295 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000296 Error = "Error reading Function level block!";
Chris Lattnerd6b65252001-10-24 01:15:12 +0000297 delete M; return failure(true);
298 }
Chris Lattner00950542001-06-06 20:29:01 +0000299
300 switch (Type) {
301 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000302 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
303 if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000304 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000305 }
306 break;
307
308 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000309 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000310 BasicBlock *BB;
311 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner05950c32001-10-13 06:47:01 +0000312 insertValue(BB, Values) == -1) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000313 delete M; return failure(true); // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
315
316 M->getBasicBlocks().push_back(BB);
317 break;
318 }
319
320 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000321 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
322 if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000323 delete M; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000324 }
325 break;
326
327 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000328 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000329 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000330 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000331 break;
332 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000333 BCR_TRACE(2, "} end block\n");
334
Chris Lattner00950542001-06-06 20:29:01 +0000335 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000336 Error = "Error aligning Function level block!";
Chris Lattner00950542001-06-06 20:29:01 +0000337 delete M; // Malformed bc file, read past end of block.
Chris Lattner3d3f2892001-07-28 17:50:18 +0000338 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000339 }
340 }
341
342 if (postResolveValues(LateResolveValues) ||
343 postResolveValues(LateResolveModuleValues)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000344 Error = "Error resolving method values!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000345 delete M; return failure(true); // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000346 }
347
Chris Lattneref9c23f2001-10-03 14:53:21 +0000348 Value *MethPHolder = getValue(PMTy, MethSlot, false);
Chris Lattner00950542001-06-06 20:29:01 +0000349 assert(MethPHolder && "Something is broken no placeholder found!");
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000350 assert(isa<Function>(MethPHolder) && "Not a function?");
Chris Lattner00950542001-06-06 20:29:01 +0000351
352 unsigned type; // Type slot
353 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000354 getTypeSlot(PMTy, type);
Chris Lattner00950542001-06-06 20:29:01 +0000355
Chris Lattner79df7c02002-03-26 18:01:55 +0000356 C->getFunctionList().push_back(M);
Chris Lattner00950542001-06-06 20:29:01 +0000357
358 // Replace placeholder with the real method pointer...
359 ModuleValues[type][MethSlot] = M;
360
Chris Lattnere4d71a12001-09-14 22:03:42 +0000361 // Clear out method level types...
362 MethodTypeValues.clear();
363
Chris Lattner00950542001-06-06 20:29:01 +0000364 // If anyone is using the placeholder make them use the real method instead
365 MethPHolder->replaceAllUsesWith(M);
366
367 // We don't need the placeholder anymore!
368 delete MethPHolder;
369
Chris Lattnerb847f512001-10-14 23:28:41 +0000370 // If the method is empty, we don't need the method argument entries...
371 if (M->isExternal())
372 M->getArgumentList().delete_all();
373
Chris Lattner05950c32001-10-13 06:47:01 +0000374 DeclareNewGlobalValue(M, MethSlot);
375
Chris Lattner00950542001-06-06 20:29:01 +0000376 return false;
377}
378
379bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Chris Lattner05950c32001-10-13 06:47:01 +0000380 Module *Mod) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000381 if (!MethodSignatureList.empty()) {
382 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000383 return failure(true); // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000384 }
Chris Lattner00950542001-06-06 20:29:01 +0000385
Chris Lattner70cc3392001-09-10 07:58:01 +0000386 // Read global variables...
387 unsigned VarType;
388 if (read_vbr(Buf, End, VarType)) return failure(true);
389 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000390 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
391 // bit2 = isInternal, bit3+ = slot#
392 const Type *Ty = getType(VarType >> 3);
Chris Lattner70cc3392001-09-10 07:58:01 +0000393 if (!Ty || !Ty->isPointerType()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000394 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner70cc3392001-09-10 07:58:01 +0000395 return failure(true);
396 }
397
Chris Lattneref9c23f2001-10-03 14:53:21 +0000398 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000399 const Type *ElTy = PTy->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000400
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000401 Constant *Initializer = 0;
Chris Lattnerd70684f2001-09-18 04:01:05 +0000402 if (VarType & 2) { // Does it have an initalizer?
403 // Do not improvise... values must have been stored in the constant pool,
404 // which should have been read before now.
405 //
406 unsigned InitSlot;
407 if (read_vbr(Buf, End, InitSlot)) return failure(true);
408
Chris Lattner05950c32001-10-13 06:47:01 +0000409 Value *V = getValue(ElTy, InitSlot, false);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000410 if (V == 0) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000411 Initializer = cast<Constant>(V);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000412 }
413
Chris Lattner70cc3392001-09-10 07:58:01 +0000414 // Create the global variable...
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000415 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
416 Initializer);
Chris Lattner05950c32001-10-13 06:47:01 +0000417 int DestSlot = insertValue(GV, ModuleValues);
418 if (DestSlot == -1) return failure(true);
419
420 Mod->getGlobalList().push_back(GV);
421
422 DeclareNewGlobalValue(GV, unsigned(DestSlot));
423
424 BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
Chris Lattner697954c2002-01-20 22:54:45 +0000425 << " into slot #" << DestSlot << "\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000426
427 if (read_vbr(Buf, End, VarType)) return failure(true);
Chris Lattner70cc3392001-09-10 07:58:01 +0000428 }
429
Chris Lattner00950542001-06-06 20:29:01 +0000430 // Read the method signatures for all of the methods that are coming, and
431 // create fillers in the Value tables.
432 unsigned MethSignature;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000433 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000434 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
435 const Type *Ty = getType(MethSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000436 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000437 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
438 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner3d3f2892001-07-28 17:50:18 +0000439 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000440 }
Chris Lattneref9c23f2001-10-03 14:53:21 +0000441
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000442 // We create methods by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000443 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000444
Chris Lattner1d670cc2001-09-07 16:37:43 +0000445 // When the ModuleGlobalInfo section is read, we load the type of each
446 // method and the 'ModuleValues' slot that it lands in. We then load a
447 // placeholder into its slot to reserve it. When the method is loaded, this
448 // placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000449
450 // Insert the placeholder...
Chris Lattneref9c23f2001-10-03 14:53:21 +0000451 Value *Val = new MethPHolder(Ty, 0);
Chris Lattner05950c32001-10-13 06:47:01 +0000452 if (insertValue(Val, ModuleValues) == -1) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000453
454 // Figure out which entry of its typeslot it went into...
455 unsigned TypeSlot;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000456 if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000457
458 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
459
460 // Keep track of this information in a linked list that is emptied as
461 // methods are loaded...
462 //
Chris Lattneref9c23f2001-10-03 14:53:21 +0000463 MethodSignatureList.push_back(
464 make_pair(cast<const PointerType>(Val->getType()), SlotNo));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000465 if (read_vbr(Buf, End, MethSignature)) return failure(true);
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000466 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000467 }
468
Chris Lattner3d3f2892001-07-28 17:50:18 +0000469 if (align32(Buf, End)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000470
471 // This is for future proofing... in the future extra fields may be added that
472 // we don't understand, so we transparently ignore them.
473 //
474 Buf = End;
475 return false;
476}
477
478bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
479 Module *&C) {
480
481 unsigned Type, Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000482 if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000483 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
484 Error = "Expected Module packet!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000485 return failure(true); // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000486 }
Chris Lattner00950542001-06-06 20:29:01 +0000487
Chris Lattner1d670cc2001-09-07 16:37:43 +0000488 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000489 MethodSignatureList.clear(); // Just in case...
490
491 // Read into instance variables...
Chris Lattner3d3f2892001-07-28 17:50:18 +0000492 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
493 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000494 BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000495
Chris Lattner05950c32001-10-13 06:47:01 +0000496 TheModule = C = new Module();
Chris Lattner00950542001-06-06 20:29:01 +0000497 while (Buf < EndBuf) {
498 const uchar *OldBuf = Buf;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000499 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000500 switch (Type) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000501 case BytecodeFormat::ConstantPool:
502 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
503 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000504 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000505 }
506 break;
507
Chris Lattner1d670cc2001-09-07 16:37:43 +0000508 case BytecodeFormat::ModuleGlobalInfo:
509 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
510
511 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000512 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000513 }
514 break;
515
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000516 case BytecodeFormat::Function: {
517 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000518 if (ParseMethod(Buf, Buf+Size, C)) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000519 delete C; return failure(true); // Error parsing method
Chris Lattner00950542001-06-06 20:29:01 +0000520 }
521 break;
522 }
523
524 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000525 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
526 if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000527 delete C; return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000528 }
529 break;
530
531 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000532 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000533 Buf += Size;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000534 if (OldBuf > Buf) return failure(true); // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000535 break;
536 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000537 BCR_TRACE(1, "} end block\n");
Chris Lattner3d3f2892001-07-28 17:50:18 +0000538 if (align32(Buf, EndBuf)) { delete C; return failure(true); }
Chris Lattner00950542001-06-06 20:29:01 +0000539 }
540
Chris Lattnerd6b65252001-10-24 01:15:12 +0000541 if (!MethodSignatureList.empty()) { // Expected more methods!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000542 Error = "Function expected, but bytecode stream at end!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000543 return failure(true);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000544 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000545
546 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000547 return false;
548}
549
550Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
551 LateResolveValues.clear();
552 unsigned Sig;
553 // Read and check signature...
554 if (read(Buf, EndBuf, Sig) ||
Chris Lattnerd6b65252001-10-24 01:15:12 +0000555 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
556 Error = "Invalid bytecode signature!";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000557 return failure<Module*>(0); // Invalid signature!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000558 }
Chris Lattner00950542001-06-06 20:29:01 +0000559
560 Module *Result;
561 if (ParseModule(Buf, EndBuf, Result)) return 0;
562 return Result;
563}
564
565
566Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
567 BytecodeParser Parser;
568 return Parser.ParseBytecode(Buffer, Buffer+Length);
569}
570
571// Parse and return a class file...
572//
Chris Lattner697954c2002-01-20 22:54:45 +0000573Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000574 struct stat StatBuf;
575 Module *Result = 0;
576
Chris Lattner697954c2002-01-20 22:54:45 +0000577 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb49ff5c2001-07-23 18:51:23 +0000578 int FD = open(Filename.c_str(), O_RDONLY);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000579 if (FD == -1) {
580 if (ErrorStr) *ErrorStr = "Error opening file!";
581 return failure<Module*>(0);
582 }
Chris Lattner00950542001-06-06 20:29:01 +0000583
Chris Lattner3d3f2892001-07-28 17:50:18 +0000584 if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000585
586 int Length = StatBuf.st_size;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000587 if (Length == 0) {
588 if (ErrorStr) *ErrorStr = "Error stat'ing file!";
589 close(FD); return failure<Module*>(0);
590 }
Chris Lattner00950542001-06-06 20:29:01 +0000591 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
592 MAP_PRIVATE, FD, 0);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000593 if (Buffer == (uchar*)-1) {
594 if (ErrorStr) *ErrorStr = "Error mmapping file!";
595 close(FD); return failure<Module*>(0);
596 }
Chris Lattner00950542001-06-06 20:29:01 +0000597
598 BytecodeParser Parser;
599 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
600
601 munmap((char*)Buffer, Length);
602 close(FD);
Chris Lattnerd6b65252001-10-24 01:15:12 +0000603 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000604 } else { // Read from stdin
605 size_t FileSize = 0;
606 int BlockSize;
607 uchar Buffer[4096], *FileData = 0;
608 while ((BlockSize = read(0, Buffer, 4))) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000609 if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
Chris Lattner00950542001-06-06 20:29:01 +0000610
611 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
612 memcpy(FileData+FileSize, Buffer, BlockSize);
613 FileSize += BlockSize;
614 }
615
Chris Lattnerd6b65252001-10-24 01:15:12 +0000616 if (FileSize == 0) {
617 if (ErrorStr) *ErrorStr = "Standard Input empty!";
618 free(FileData); return failure<Module*>(0);
619 }
Chris Lattner00950542001-06-06 20:29:01 +0000620
621#define ALIGN_PTRS 1
622#if ALIGN_PTRS
623 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
624 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
625 assert((Buf != (uchar*)-1) && "mmap returned error!");
Chris Lattner00950542001-06-06 20:29:01 +0000626 memcpy(Buf, FileData, FileSize);
Chris Lattnerb7325432001-11-12 20:30:12 +0000627 free(FileData);
Chris Lattner00950542001-06-06 20:29:01 +0000628#else
629 uchar *Buf = FileData;
630#endif
631
632 BytecodeParser Parser;
633 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
634
635#if ALIGN_PTRS
636 munmap((char*)Buf, FileSize); // Free mmap'd data area
637#else
638 free(FileData); // Free realloc'd block of memory
639#endif
Chris Lattnerd6b65252001-10-24 01:15:12 +0000640
641 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattner00950542001-06-06 20:29:01 +0000642 }
643
644 return Result;
645}