blob: 727e2a91100ffc66e29b62f8b86935075d9fca5a [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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000017#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iOther.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000019#include "llvm/Module.h"
20#include "Support/StringExtras.h"
John Criswell7a73b802003-06-30 21:59:07 +000021#include "Config/unistd.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000022#include "Config/sys/mman.h"
23#include "Config/sys/stat.h"
24#include "Config/sys/types.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include <algorithm>
Misha Brukman12c29d12003-09-22 23:38:23 +000026#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000027
Misha Brukmane0dd0d42003-09-23 16:15:29 +000028static inline void ALIGN32(const unsigned char *&begin,
29 const unsigned char *end) {
30 if (align32(begin, end))
31 throw std::string("Alignment error in buffer: read past end of block.");
32}
Misha Brukman12c29d12003-09-22 23:38:23 +000033
Chris Lattner9e460f22003-10-04 20:00:03 +000034unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
35 if (Ty->isPrimitiveType())
36 return Ty->getPrimitiveID();
37
38 // Check the function level types first...
39 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
40 FunctionTypeValues.end(), Ty);
41 if (I != FunctionTypeValues.end())
42 return FirstDerivedTyID + ModuleTypeValues.size() +
43 (&*I - &FunctionTypeValues[0]);
44
45 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
46 if (I == ModuleTypeValues.end())
47 throw std::string("Didn't find type in ModuleTypeValues.");
48 return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Chris Lattner00950542001-06-06 20:29:01 +000049}
50
51const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000052 if (ID < Type::NumPrimitiveIDs) {
53 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
54 if (T) return T;
55 }
Chris Lattner00950542001-06-06 20:29:01 +000056
Chris Lattner697954c2002-01-20 22:54:45 +000057 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000058 const Value *V = getValue(Type::TypeTy, ID, false);
59 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000060}
61
Chris Lattner52e20b02003-03-19 20:54:26 +000062int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
63 assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
64 Val->getType()->isPrimitiveType() ||
65 !cast<Constant>(Val)->isNullValue()) &&
66 "Cannot read null values from bytecode!");
Chris Lattner9e460f22003-10-04 20:00:03 +000067 unsigned type = getTypeSlot(Val->getType());
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
Chris Lattner52e20b02003-03-19 20:54:26 +000070 if (ValueTab.size() <= type) {
71 unsigned OldSize = ValueTab.size();
72 ValueTab.resize(type+1);
73 while (OldSize != type+1)
74 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +000075 }
Chris Lattner00950542001-06-06 20:29:01 +000076
77 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Misha Brukman12c29d12003-09-22 23:38:23 +000078 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000079 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000080
Chris Lattner52e20b02003-03-19 20:54:26 +000081 bool HasOffset = HasImplicitZeroInitializer &&
Misha Brukman12c29d12003-09-22 23:38:23 +000082 !Val->getType()->isPrimitiveType();
Chris Lattner52e20b02003-03-19 20:54:26 +000083
84 return ValueTab[type]->size()-1 + HasOffset;
85}
86
87
88void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
89 Value *Val) {
90 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
Chris Lattner52e20b02003-03-19 20:54:26 +000091 assert((!HasImplicitZeroInitializer || Slot != 0) &&
92 "Cannot change zero init");
Chris Lattner9e460f22003-10-04 20:00:03 +000093 unsigned type = getTypeSlot(Val->getType());
Chris Lattner52e20b02003-03-19 20:54:26 +000094 assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
95 ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
Chris Lattner00950542001-06-06 20:29:01 +000096}
97
98Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
99 unsigned Num = oNum;
Chris Lattner9e460f22003-10-04 20:00:03 +0000100 unsigned type = getTypeSlot(Ty); // The type plane it lives in...
Chris Lattner00950542001-06-06 20:29:01 +0000101
Misha Brukman12c29d12003-09-22 23:38:23 +0000102 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000104 if (Num < Type::NumPrimitiveIDs) {
105 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
106 if (T) return (Value*)T; // Asked for a primitive type...
107 }
Chris Lattner00950542001-06-06 20:29:01 +0000108
109 // Otherwise, derived types need offset...
110 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111
Misha Brukman12c29d12003-09-22 23:38:23 +0000112 // Is it a module-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000113 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000114 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000115
Misha Brukman12c29d12003-09-22 23:38:23 +0000116 // Nope, is it a function-level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117 Num -= ModuleTypeValues.size();
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000118 if (Num < FunctionTypeValues.size())
119 return (Value*)FunctionTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120
121 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000122 }
123
Chris Lattner52e20b02003-03-19 20:54:26 +0000124 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
125 if (Num == 0)
126 return Constant::getNullValue(Ty);
127 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000128 }
129
Chris Lattner52e20b02003-03-19 20:54:26 +0000130 if (type < ModuleValues.size()) {
131 if (Num < ModuleValues[type]->size())
132 return ModuleValues[type]->getOperand(Num);
133 Num -= ModuleValues[type]->size();
134 }
135
136 if (Values.size() > type && Values[type]->size() > Num)
137 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattner74734132002-08-17 22:01:27 +0000139 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 Value *d = 0;
142 switch (Ty->getPrimitiveID()) {
Chris Lattner74734132002-08-17 22:01:27 +0000143 case Type::LabelTyID:
144 d = new BBPHolder(Ty, oNum);
145 break;
146 default:
147 d = new ValPHolder(Ty, oNum);
148 break;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150
151 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000152 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000153 return d;
154}
155
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000156/// getConstantValue - Just like getValue, except that it returns a null pointer
157/// only on error. It always returns a constant (meaning that if the value is
158/// defined, but is not a constant, that is an error). If the specified
159/// constant hasn't been parsed yet, a placeholder is defined and used. Later,
160/// after the real value is parsed, the placeholder is eliminated.
161///
162Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
163 if (Value *V = getValue(Ty, Slot, false))
164 return dyn_cast<Constant>(V); // If we already have the value parsed...
165
Chris Lattner52e20b02003-03-19 20:54:26 +0000166 std::pair<const Type*, unsigned> Key(Ty, Slot);
167 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
168
169 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000170 BCR_TRACE(5, "Previous forward ref found!\n");
171 return cast<Constant>(I->second);
172 } else {
173 // Create a placeholder for the constant reference and
174 // keep track of the fact that we have a forward ref to recycle it
175 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
176 Constant *C = new ConstPHolder(Ty, Slot);
177
178 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner52e20b02003-03-19 20:54:26 +0000179 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000180 return C;
181 }
182}
183
184
Misha Brukman12c29d12003-09-22 23:38:23 +0000185void BytecodeParser::postResolveValues(ValueTable &ValTab) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000186 while (!ValTab.empty()) {
Chris Lattner51ca8602003-10-04 19:29:21 +0000187 ValueList &VL = *ValTab.back();
Chris Lattner52e20b02003-03-19 20:54:26 +0000188 ValTab.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattner51ca8602003-10-04 19:29:21 +0000190 while (!VL.empty()) {
191 Value *V = VL.back();
192 unsigned IDNumber = getValueIDNumberFromPlaceHolder(V);
193 VL.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattner51ca8602003-10-04 19:29:21 +0000195 Value *NewVal = getValue(V->getType(), IDNumber, false);
196 if (NewVal == 0)
Misha Brukman12c29d12003-09-22 23:38:23 +0000197 throw std::string("Unresolvable reference found: <" +
Chris Lattner51ca8602003-10-04 19:29:21 +0000198 V->getType()->getDescription() + ">:" +
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000199 utostr(IDNumber) + ".");
Chris Lattner00950542001-06-06 20:29:01 +0000200
Chris Lattner51ca8602003-10-04 19:29:21 +0000201 // Fixup all of the uses of this placeholder def...
202 V->replaceAllUsesWith(NewVal);
203
204 // Now that all the uses are gone, delete the placeholder...
205 // If we couldn't find a def (error case), then leak a little
206 delete V; // memory, 'cause otherwise we can't remove all uses!
Chris Lattner00950542001-06-06 20:29:01 +0000207 }
Chris Lattner51ca8602003-10-04 19:29:21 +0000208 delete &VL;
Chris Lattner00950542001-06-06 20:29:01 +0000209 }
Chris Lattner00950542001-06-06 20:29:01 +0000210}
211
Misha Brukman12c29d12003-09-22 23:38:23 +0000212std::auto_ptr<BasicBlock>
213BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
214 const unsigned char *EndBuf) {
215 std::auto_ptr<BasicBlock> BB(new BasicBlock());
Chris Lattner00950542001-06-06 20:29:01 +0000216
217 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000218 Instruction *Inst;
Misha Brukman12c29d12003-09-22 23:38:23 +0000219 ParseInstruction(Buf, EndBuf, Inst);
220
221 if (Inst == 0) { throw std::string("Could not parse Instruction."); }
222 if (insertValue(Inst, Values) == -1) {
223 throw std::string("Could not insert value.");
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattner1d670cc2001-09-07 16:37:43 +0000226 BB->getInstList().push_back(Inst);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000227 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000228 }
229
Misha Brukman12c29d12003-09-22 23:38:23 +0000230 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000231}
232
Misha Brukman12c29d12003-09-22 23:38:23 +0000233void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000234 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000235 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000236 while (Buf < EndBuf) {
237 // Symtab block header: [num entries][type id number]
238 unsigned NumEntries, Typ;
239 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000240 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000241 const Type *Ty = getType(Typ);
Misha Brukman12c29d12003-09-22 23:38:23 +0000242 if (Ty == 0) throw std::string("Invalid type read in symbol table.");
Chris Lattner00950542001-06-06 20:29:01 +0000243
Chris Lattner1d670cc2001-09-07 16:37:43 +0000244 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
Misha Brukman12c29d12003-09-22 23:38:23 +0000245 " entries\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000246
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000247 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000248 // Symtab entry: [def slot #][name]
249 unsigned slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000250 if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
Chris Lattner697954c2002-01-20 22:54:45 +0000251 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000252 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Misha Brukman12c29d12003-09-22 23:38:23 +0000253 throw std::string("Buffer not aligned.");
Chris Lattner00950542001-06-06 20:29:01 +0000254
Chris Lattner52e20b02003-03-19 20:54:26 +0000255 Value *V = getValue(Ty, slot, false); // Find mapping...
256 if (V == 0) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000257 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
258 throw std::string("Failed value look-up.");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000259 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000260 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
Misha Brukman12c29d12003-09-22 23:38:23 +0000261 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000262
Chris Lattner52e20b02003-03-19 20:54:26 +0000263 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000264 }
265 }
266
Misha Brukman12c29d12003-09-22 23:38:23 +0000267 if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000268}
269
Chris Lattner74734132002-08-17 22:01:27 +0000270void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000271 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
272 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000273 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000274
Chris Lattner74734132002-08-17 22:01:27 +0000275 BCR_TRACE(3, "Mutating forward refs!\n");
276 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000277
Chris Lattner52e20b02003-03-19 20:54:26 +0000278 VPH->replaceAllUsesWith(NewV);
279
280 // If this is a global variable being resolved, remove the placeholder from
281 // the module...
282 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
283 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000284
Chris Lattner74734132002-08-17 22:01:27 +0000285 delete VPH; // Delete the old placeholder
286 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000287}
288
Misha Brukman12c29d12003-09-22 23:38:23 +0000289void
290BytecodeParser::ParseFunction(const unsigned char *&Buf,
291 const unsigned char *EndBuf) {
292 if (FunctionSignatureList.empty())
293 throw std::string("FunctionSignatureList empty!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000294
Misha Brukman12c29d12003-09-22 23:38:23 +0000295 Function *F = FunctionSignatureList.back().first;
296 unsigned FunctionSlot = FunctionSignatureList.back().second;
297 FunctionSignatureList.pop_back();
298
299 // Save the information for future reading of the function
300 LazyFunctionInfo *LFI = new LazyFunctionInfo();
301 LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
302 LazyFunctionLoadMap[F] = LFI;
303 // Pretend we've `parsed' this function
304 Buf = EndBuf;
305}
306
307void BytecodeParser::materializeFunction(Function* F) {
308 // Find {start, end} pointers and slot in the map. If not there, we're done.
309 std::map<Function*, LazyFunctionInfo*>::iterator Fi =
310 LazyFunctionLoadMap.find(F);
311 if (Fi == LazyFunctionLoadMap.end()) return;
312
313 LazyFunctionInfo *LFI = Fi->second;
314 const unsigned char *Buf = LFI->Buf;
315 const unsigned char *EndBuf = LFI->EndBuf;
316 unsigned FunctionSlot = LFI->FunctionSlot;
317 LazyFunctionLoadMap.erase(Fi);
318 delete LFI;
Chris Lattner00950542001-06-06 20:29:01 +0000319
Chris Lattnere3869c82003-04-16 21:16:05 +0000320 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
321
322 if (!hasInternalMarkerOnly) {
323 unsigned LinkageType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000324 if (read_vbr(Buf, EndBuf, LinkageType))
325 throw std::string("ParseFunction: Error reading from buffer.");
326 if (LinkageType & ~0x3)
327 throw std::string("Invalid linkage type for Function.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000328 Linkage = (GlobalValue::LinkageTypes)LinkageType;
329 } else {
330 // We used to only support two linkage models: internal and external
331 unsigned isInternal;
Misha Brukman12c29d12003-09-22 23:38:23 +0000332 if (read_vbr(Buf, EndBuf, isInternal))
333 throw std::string("ParseFunction: Error reading from buffer.");
Chris Lattnere3869c82003-04-16 21:16:05 +0000334 if (isInternal) Linkage = GlobalValue::InternalLinkage;
335 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000336
Chris Lattnere3869c82003-04-16 21:16:05 +0000337 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000338
Chris Lattner52e20b02003-03-19 20:54:26 +0000339 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
340 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000341 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000342 It != Params.end(); ++It, ++AI) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000343 if (insertValue(AI, Values) == -1)
344 throw std::string("Error reading function arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000345 }
346
347 while (Buf < EndBuf) {
348 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000349 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000350 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000351
352 switch (Type) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000353 case BytecodeFormat::ConstantPool: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000354 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000355 ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000356 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000357 }
Chris Lattner00950542001-06-06 20:29:01 +0000358
359 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000360 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000361 std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
362 if (!BB.get() || insertValue(BB.get(), Values) == -1)
363 throw std::string("Parse error: BasicBlock");
Chris Lattner00950542001-06-06 20:29:01 +0000364
Misha Brukman12c29d12003-09-22 23:38:23 +0000365 F->getBasicBlockList().push_back(BB.release());
Chris Lattner00950542001-06-06 20:29:01 +0000366 break;
367 }
368
Misha Brukman12c29d12003-09-22 23:38:23 +0000369 case BytecodeFormat::SymbolTable: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000370 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000371 ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000372 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000373 }
Chris Lattner00950542001-06-06 20:29:01 +0000374
375 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000376 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000377 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000378 if (OldBuf > Buf)
379 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +0000380 break;
381 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000382 BCR_TRACE(2, "} end block\n");
383
Misha Brukman12c29d12003-09-22 23:38:23 +0000384 // Malformed bc file if read past end of block.
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000385 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000386 }
387
Misha Brukman12c29d12003-09-22 23:38:23 +0000388 // Check for unresolvable references
389 postResolveValues(LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000390
Misha Brukman12c29d12003-09-22 23:38:23 +0000391 //ResolveReferencesToValue(F, FunctionSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000392
Misha Brukman12c29d12003-09-22 23:38:23 +0000393 // Clear out function-level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000394 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000395
Chris Lattner52e20b02003-03-19 20:54:26 +0000396 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000397}
398
Misha Brukman12c29d12003-09-22 23:38:23 +0000399void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
400 const unsigned char *End) {
401 if (!FunctionSignatureList.empty())
402 throw std::string("Two ModuleGlobalInfo packets found!");
Chris Lattner00950542001-06-06 20:29:01 +0000403
Chris Lattner70cc3392001-09-10 07:58:01 +0000404 // Read global variables...
405 unsigned VarType;
Misha Brukman12c29d12003-09-22 23:38:23 +0000406 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000407 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000408 unsigned SlotNo;
409 GlobalValue::LinkageTypes Linkage;
410
411 if (!hasInternalMarkerOnly) {
412 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
413 // bit2,3 = Linkage, bit4+ = slot#
414 SlotNo = VarType >> 4;
415 Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
416 } else {
417 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
418 // bit2 = isInternal, bit3+ = slot#
419 SlotNo = VarType >> 3;
420 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
421 GlobalValue::ExternalLinkage;
422 }
423
424 const Type *Ty = getType(SlotNo);
Misha Brukman12c29d12003-09-22 23:38:23 +0000425 if (!Ty || !isa<PointerType>(Ty))
426 throw std::string("Global not pointer type! Ty = " +
427 Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +0000428
Chris Lattner52e20b02003-03-19 20:54:26 +0000429 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000430
Chris Lattner70cc3392001-09-10 07:58:01 +0000431 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000432 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000433 0, "", TheModule);
Chris Lattner05950c32001-10-13 06:47:01 +0000434 int DestSlot = insertValue(GV, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000435 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000436 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000437 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000438
Misha Brukman37f92e22003-09-11 22:34:13 +0000439 if (VarType & 2) { // Does it have an initializer?
Chris Lattner52e20b02003-03-19 20:54:26 +0000440 unsigned InitSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000441 if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000442 GlobalInits.push_back(std::make_pair(GV, InitSlot));
443 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000444 if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
Chris Lattner70cc3392001-09-10 07:58:01 +0000445 }
446
Chris Lattner52e20b02003-03-19 20:54:26 +0000447 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000448 unsigned FnSignature;
Misha Brukman12c29d12003-09-22 23:38:23 +0000449 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattner74734132002-08-17 22:01:27 +0000450 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
451 const Type *Ty = getType(FnSignature);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000452 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000453 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000454 throw std::string("Function not ptr to func type! Ty = " +
455 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000456 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000457
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000458 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000459 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000460
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000461 // When the ModuleGlobalInfo section is read, we load the type of each
462 // function and the 'ModuleValues' slot that it lands in. We then load a
463 // placeholder into its slot to reserve it. When the function is loaded,
464 // this placeholder is replaced.
Chris Lattner00950542001-06-06 20:29:01 +0000465
466 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000467 Function *Func = new Function(cast<FunctionType>(Ty),
468 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000469 int DestSlot = insertValue(Func, ModuleValues);
Misha Brukman12c29d12003-09-22 23:38:23 +0000470 if (DestSlot == -1) throw Error_DestSlot;
Chris Lattner52e20b02003-03-19 20:54:26 +0000471 ResolveReferencesToValue(Func, (unsigned)DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000472
Chris Lattner52e20b02003-03-19 20:54:26 +0000473 // Keep track of this information in a list that is emptied as functions are
474 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000475 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000476 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
477
Misha Brukman12c29d12003-09-22 23:38:23 +0000478 if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000479 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000480 }
481
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000482 ALIGN32(Buf, End);
Chris Lattner74734132002-08-17 22:01:27 +0000483
484 // Now that the function signature list is set up, reverse it so that we can
485 // remove elements efficiently from the back of the vector.
486 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +0000487
488 // This is for future proofing... in the future extra fields may be added that
489 // we don't understand, so we transparently ignore them.
490 //
491 Buf = End;
Chris Lattner00950542001-06-06 20:29:01 +0000492}
493
Misha Brukman12c29d12003-09-22 23:38:23 +0000494void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000495 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000496 unsigned Version;
Misha Brukman12c29d12003-09-22 23:38:23 +0000497 if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000498
499 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000500 Module::Endianness Endianness;
501 Module::PointerSize PointerSize;
502 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
503 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
504
505 bool hasNoEndianness = Version & 4;
506 bool hasNoPointerSize = Version & 8;
507
508 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000509
510 // Default values for the current bytecode version
Chris Lattner036b8aa2003-03-06 17:55:45 +0000511 HasImplicitZeroInitializer = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000512 hasInternalMarkerOnly = false;
513 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000514
515 switch (RevisionNum) {
516 case 0: // Initial revision
Chris Lattner52e20b02003-03-19 20:54:26 +0000517 // Version #0 didn't have any of the flags stored correctly, and in fact as
518 // only valid with a 14 in the flags values. Also, it does not support
519 // encoding zero initializers for arrays compactly.
520 //
Misha Brukman12c29d12003-09-22 23:38:23 +0000521 if (Version != 14) throw std::string("Unknown revision 0 flags?");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000522 HasImplicitZeroInitializer = false;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000523 Endianness = Module::BigEndian;
524 PointerSize = Module::Pointer64;
Chris Lattnere3869c82003-04-16 21:16:05 +0000525 hasInternalMarkerOnly = true;
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000526 hasNoEndianness = hasNoPointerSize = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000527 break;
528 case 1:
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000529 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
530 // hasNoEndianness, and hasNoPointerSize.
Chris Lattnere3869c82003-04-16 21:16:05 +0000531 hasInternalMarkerOnly = true;
532 break;
533 case 2:
534 // Version #2 added information about all 4 linkage types instead of just
535 // having internal and external.
Chris Lattner036b8aa2003-03-06 17:55:45 +0000536 break;
537 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000538 throw std::string("Unknown bytecode version number!");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000539 }
540
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000541 if (hasNoEndianness) Endianness = Module::AnyEndianness;
542 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +0000543
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000544 TheModule->setEndianness(Endianness);
545 TheModule->setPointerSize(PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +0000546 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
Chris Lattnerd445c6b2003-08-24 13:47:36 +0000547 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
548 << PointerSize << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000549 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
Chris Lattner036b8aa2003-03-06 17:55:45 +0000550}
551
Misha Brukman12c29d12003-09-22 23:38:23 +0000552void BytecodeParser::ParseModule(const unsigned char *Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000553 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000554 unsigned Type, Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000555 readBlock(Buf, EndBuf, Type, Size);
556 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
557 throw std::string("Expected Module packet! B: "+
558 utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
559 " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
Chris Lattner00950542001-06-06 20:29:01 +0000560
Chris Lattner1d670cc2001-09-07 16:37:43 +0000561 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000562 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000563
564 // Read into instance variables...
Misha Brukman12c29d12003-09-22 23:38:23 +0000565 ParseVersionInfo(Buf, EndBuf);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000566 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000567
Chris Lattner00950542001-06-06 20:29:01 +0000568 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000569 const unsigned char *OldBuf = Buf;
Misha Brukman12c29d12003-09-22 23:38:23 +0000570 readBlock(Buf, EndBuf, Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +0000571 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000572 case BytecodeFormat::GlobalTypePlane:
573 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000574 ParseGlobalTypes(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000575 break;
576
577 case BytecodeFormat::ModuleGlobalInfo:
578 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000579 ParseModuleGlobalInfo(Buf, Buf+Size);
Chris Lattner52e20b02003-03-19 20:54:26 +0000580 break;
581
Chris Lattner1d670cc2001-09-07 16:37:43 +0000582 case BytecodeFormat::ConstantPool:
583 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000584 ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
Chris Lattner00950542001-06-06 20:29:01 +0000585 break;
586
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000587 case BytecodeFormat::Function: {
588 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000589 ParseFunction(Buf, Buf+Size);
Chris Lattner00950542001-06-06 20:29:01 +0000590 break;
591 }
592
593 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000594 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000595 ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000596 break;
597
598 default:
Chris Lattner00950542001-06-06 20:29:01 +0000599 Buf += Size;
Misha Brukman12c29d12003-09-22 23:38:23 +0000600 if (OldBuf > Buf) throw std::string("Expected Module Block!");
Chris Lattner00950542001-06-06 20:29:01 +0000601 break;
602 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000603 BCR_TRACE(1, "} end block\n");
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000604 ALIGN32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +0000605 }
606
Chris Lattner52e20b02003-03-19 20:54:26 +0000607 // After the module constant pool has been read, we can safely initialize
608 // global variables...
609 while (!GlobalInits.empty()) {
610 GlobalVariable *GV = GlobalInits.back().first;
611 unsigned Slot = GlobalInits.back().second;
612 GlobalInits.pop_back();
613
614 // Look up the initializer value...
615 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000616 if (GV->hasInitializer())
617 throw std::string("Global *already* has an initializer?!");
Chris Lattner52e20b02003-03-19 20:54:26 +0000618 GV->setInitializer(cast<Constant>(V));
619 } else
Misha Brukman12c29d12003-09-22 23:38:23 +0000620 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +0000621 }
622
Misha Brukman12c29d12003-09-22 23:38:23 +0000623 if (!FunctionSignatureList.empty())
624 throw std::string("Function expected, but bytecode stream ended!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000625
626 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000627}
628
Misha Brukman12c29d12003-09-22 23:38:23 +0000629void
630BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
631 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000632
Misha Brukman12c29d12003-09-22 23:38:23 +0000633 unsigned char *EndBuf = (unsigned char*)(Buf + Length);
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000634
Chris Lattner00950542001-06-06 20:29:01 +0000635 // Read and check signature...
Misha Brukmane0dd0d42003-09-23 16:15:29 +0000636 unsigned Sig;
Chris Lattner00950542001-06-06 20:29:01 +0000637 if (read(Buf, EndBuf, Sig) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000638 Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
639 throw std::string("Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000640
Chris Lattner75f20532003-04-22 18:02:52 +0000641 TheModule = new Module(ModuleID);
Misha Brukman12c29d12003-09-22 23:38:23 +0000642 try {
643 ParseModule(Buf, EndBuf);
644 } catch (std::string &Error) {
Chris Lattnera2602f32003-05-22 18:26:48 +0000645 freeState(); // Must destroy handles before deleting module!
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000646 delete TheModule;
647 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +0000648 throw;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000649 }
Chris Lattner00950542001-06-06 20:29:01 +0000650}