1 //===- Reader.cpp - Code to read bytecode files -----------------------------===
3 // This library implements the functionality defined in llvm/Bytecode/Reader.h
5 // Note that this library should be as fast as possible, reentrant, and
8 // TODO: Make error message outputs be configurable depending on an option?
9 // TODO: Allow passing in an option to ignore the symbol table
11 //===------------------------------------------------------------------------===
13 #include "llvm/Bytecode/Reader.h"
14 #include "llvm/Bytecode/Format.h"
15 #include "llvm/Module.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/ConstPoolVals.h"
19 #include "llvm/iOther.h"
20 #include "ReaderInternals.h"
21 #include <sys/types.h>
28 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
29 if (Ty->isPrimitiveType()) {
30 Slot = Ty->getPrimitiveID();
32 TypeMapType::iterator I = TypeMap.find(Ty);
33 if (I == TypeMap.end()) return true; // Didn't find type!
36 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
40 const Type *BytecodeParser::getType(unsigned ID) {
41 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
44 //cerr << "Looking up Type ID: " << ID << endl;
46 const Value *D = getValue(Type::TypeTy, ID, false);
49 assert(D->getType() == Type::TypeTy);
50 return ((const ConstPoolType*)D->castConstantAsserting())->getValue();
53 bool BytecodeParser::insertValue(Value *Def, vector<ValueList> &ValueTab) {
55 if (getTypeSlot(Def->getType(), type)) return true;
57 if (ValueTab.size() <= type)
58 ValueTab.resize(type+1, ValueList());
60 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
61 // << "] = " << Def << endl;
63 if (type == Type::TypeTyID && Def->isConstant()) {
64 const Type *Ty = ((const ConstPoolType*)Def)->getValue();
65 unsigned ValueOffset = FirstDerivedTyID;
67 if (&ValueTab == &Values) // Take into consideration module level types
68 ValueOffset += ModuleValues[type].size();
70 if (TypeMap.find(Ty) == TypeMap.end())
71 TypeMap[Ty] = ValueTab[type].size()+ValueOffset;
74 ValueTab[type].push_back(Def);
79 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
81 unsigned type; // The type plane it lives in...
83 if (getTypeSlot(Ty, type)) return 0; // TODO: true
85 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
86 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
87 if (T) return (Value*)T; // Asked for a primitive type...
89 // Otherwise, derived types need offset...
90 Num -= FirstDerivedTyID;
93 if (ModuleValues.size() > type) {
94 if (ModuleValues[type].size() > Num)
95 return ModuleValues[type][Num];
96 Num -= ModuleValues[type].size();
99 if (Values.size() > type && Values[type].size() > Num)
100 return Values[type][Num];
102 if (!Create) return 0; // Do not create a placeholder?
105 switch (Ty->getPrimitiveID()) {
106 case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
107 case Type::MethodTyID:
108 cerr << "Creating method pholder! : " << type << ":" << oNum << " "
109 << Ty->getName() << endl;
110 d = new MethPHolder(Ty, oNum);
111 insertValue(d, LateResolveModuleValues);
113 default: d = new DefPHolder(Ty, oNum); break;
116 assert(d != 0 && "How did we not make something?");
117 if (insertValue(d, LateResolveValues)) return 0;
121 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
123 for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
124 ValueList &DL = ValTab[ty];
126 while ((Size = DL.size())) {
127 unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
129 Value *D = DL[Size-1];
132 Value *NewDef = getValue(D->getType(), IDNumber, false);
134 Error = true; // Unresolved thinger
135 cerr << "Unresolvable reference found: <" << D->getType()->getName()
136 << ">:" << IDNumber << "!\n";
138 // Fixup all of the uses of this placeholder def...
139 D->replaceAllUsesWith(NewDef);
141 // Now that all the uses are gone, delete the placeholder...
142 // If we couldn't find a def (error case), then leak a little
143 delete D; // memory, 'cause otherwise we can't remove all uses!
151 bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
153 BB = new BasicBlock();
155 while (Buf < EndBuf) {
157 if (ParseInstruction(Buf, EndBuf, Def)) {
162 if (Def == 0) { delete BB; return true; }
163 if (insertValue(Def, Values)) { delete BB; return true; }
165 BB->getInstList().push_back(Def);
171 bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf) {
172 while (Buf < EndBuf) {
173 // Symtab block header: [num entries][type id number]
174 unsigned NumEntries, Typ;
175 if (read_vbr(Buf, EndBuf, NumEntries) ||
176 read_vbr(Buf, EndBuf, Typ)) return true;
177 const Type *Ty = getType(Typ);
178 if (Ty == 0) return true;
180 for (unsigned i = 0; i < NumEntries; ++i) {
181 // Symtab entry: [def slot #][name]
183 if (read_vbr(Buf, EndBuf, slot)) return true;
185 if (read(Buf, EndBuf, Name, false)) // Not aligned...
188 Value *D = getValue(Ty, slot, false); // Find mapping...
189 if (D == 0) return true;
198 bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
200 // Clear out the local values table...
202 if (MethodSignatureList.empty()) return true; // Unexpected method!
204 const MethodType *MTy = MethodSignatureList.front().first;
205 unsigned MethSlot = MethodSignatureList.front().second;
206 MethodSignatureList.pop_front();
207 Method *M = new Method(MTy);
209 const MethodType::ParamTypes &Params = MTy->getParamTypes();
210 for (MethodType::ParamTypes::const_iterator It = Params.begin();
211 It != Params.end(); ++It) {
212 MethodArgument *MA = new MethodArgument(*It);
213 if (insertValue(MA, Values)) { delete M; return true; }
214 M->getArgumentList().push_back(MA);
217 while (Buf < EndBuf) {
219 const uchar *OldBuf = Buf;
220 if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return true; }
223 case BytecodeFormat::ConstantPool:
224 if (ParseConstantPool(Buf, Buf+Size, M->getConstantPool(), Values)) {
225 cerr << "Error reading constant pool!\n";
226 delete M; return true;
230 case BytecodeFormat::BasicBlock: {
232 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
233 insertValue(BB, Values)) {
234 cerr << "Error parsing basic block!\n";
235 delete M; return true; // Parse error... :(
238 M->getBasicBlocks().push_back(BB);
242 case BytecodeFormat::SymbolTable:
243 if (ParseSymbolTable(Buf, Buf+Size)) {
244 cerr << "Error reading method symbol table!\n";
245 delete M; return true;
251 if (OldBuf > Buf) return true; // Wrap around!
254 if (align32(Buf, EndBuf)) {
255 delete M; // Malformed bc file, read past end of block.
260 if (postResolveValues(LateResolveValues) ||
261 postResolveValues(LateResolveModuleValues)) {
262 delete M; return true; // Unresolvable references!
265 Value *MethPHolder = getValue(MTy, MethSlot, false);
266 assert(MethPHolder && "Something is broken no placeholder found!");
267 assert(MethPHolder->isMethod() && "Not a method?");
269 unsigned type; // Type slot
270 assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
271 getTypeSlot(MTy, type);
273 C->getMethodList().push_back(M);
275 // Replace placeholder with the real method pointer...
276 ModuleValues[type][MethSlot] = M;
278 // If anyone is using the placeholder make them use the real method instead
279 MethPHolder->replaceAllUsesWith(M);
281 // We don't need the placeholder anymore!
287 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
290 if (!MethodSignatureList.empty()) return true; // Two ModuleGlobal blocks?
292 // Read the method signatures for all of the methods that are coming, and
293 // create fillers in the Value tables.
294 unsigned MethSignature;
295 if (read_vbr(Buf, End, MethSignature)) return true;
296 while (MethSignature != Type::VoidTyID) { // List is terminated by Void
297 const Type *Ty = getType(MethSignature);
298 if (!Ty || !Ty->isMethodType()) {
299 cerr << "Method not meth type! ";
300 if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl;
304 // When the ModuleGlobalInfo section is read, we load the type of each method
305 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
306 // into its slot to reserve it. When the method is loaded, this placeholder
309 // Insert the placeholder...
310 Value *Def = new MethPHolder(Ty, 0);
311 insertValue(Def, ModuleValues);
313 // Figure out which entry of its typeslot it went into...
315 if (getTypeSlot(Def->getType(), TypeSlot)) return true;
317 unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
319 // Keep track of this information in a linked list that is emptied as
320 // methods are loaded...
322 MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
323 if (read_vbr(Buf, End, MethSignature)) return true;
326 if (align32(Buf, End)) return true;
328 // This is for future proofing... in the future extra fields may be added that
329 // we don't understand, so we transparently ignore them.
335 bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
339 if (readBlock(Buf, EndBuf, Type, Size)) return true;
340 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
341 return true; // Hrm, not a class?
343 MethodSignatureList.clear(); // Just in case...
345 // Read into instance variables...
346 if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
347 if (align32(Buf, EndBuf)) return true;
351 while (Buf < EndBuf) {
352 const uchar *OldBuf = Buf;
353 if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return true; }
355 case BytecodeFormat::ModuleGlobalInfo:
356 if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
357 cerr << "Error reading class global info section!\n";
358 delete C; return true;
362 case BytecodeFormat::ConstantPool:
363 if (ParseConstantPool(Buf, Buf+Size, C->getConstantPool(), ModuleValues)) {
364 cerr << "Error reading class constant pool!\n";
365 delete C; return true;
369 case BytecodeFormat::Method: {
370 if (ParseMethod(Buf, Buf+Size, C)) {
371 delete C; return true; // Error parsing method
376 case BytecodeFormat::SymbolTable:
377 if (ParseSymbolTable(Buf, Buf+Size)) {
378 cerr << "Error reading class symbol table!\n";
379 delete C; return true;
384 cerr << "Unknown class block: " << Type << endl;
386 if (OldBuf > Buf) return true; // Wrap around!
389 if (align32(Buf, EndBuf)) { delete C; return true; }
392 if (!MethodSignatureList.empty()) // Expected more methods!
397 Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
398 LateResolveValues.clear();
400 // Read and check signature...
401 if (read(Buf, EndBuf, Sig) ||
402 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
403 return 0; // Invalid signature!
406 if (ParseModule(Buf, EndBuf, Result)) return 0;
411 Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
412 BytecodeParser Parser;
413 return Parser.ParseBytecode(Buffer, Buffer+Length);
416 // Parse and return a class file...
418 Module *ParseBytecodeFile(const string &Filename) {
422 if (Filename != string("-")) { // Read from a file...
423 int FD = open(Filename.data(), O_RDONLY);
424 if (FD == -1) return 0;
426 if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
428 int Length = StatBuf.st_size;
429 if (Length == 0) { close(FD); return 0; }
430 uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
432 if (Buffer == (uchar*)-1) { close(FD); return 0; }
434 BytecodeParser Parser;
435 Result = Parser.ParseBytecode(Buffer, Buffer+Length);
437 munmap((char*)Buffer, Length);
439 } else { // Read from stdin
442 uchar Buffer[4096], *FileData = 0;
443 while ((BlockSize = read(0, Buffer, 4))) {
444 if (BlockSize == -1) { free(FileData); return 0; }
446 FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
447 memcpy(FileData+FileSize, Buffer, BlockSize);
448 FileSize += BlockSize;
451 if (FileSize == 0) { free(FileData); return 0; }
455 uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
456 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
457 assert((Buf != (uchar*)-1) && "mmap returned error!");
459 memcpy(Buf, FileData, FileSize);
461 uchar *Buf = FileData;
464 BytecodeParser Parser;
465 Result = Parser.ParseBytecode(Buf, Buf+FileSize);
468 munmap((char*)Buf, FileSize); // Free mmap'd data area
470 free(FileData); // Free realloc'd block of memory