-//===- Reader.cpp - Code to read bytecode files -----------------------------===
+//===- Reader.cpp - Code to read bytecode files ---------------------------===//
//
// This library implements the functionality defined in llvm/Bytecode/Reader.h
//
// TODO: Make error message outputs be configurable depending on an option?
// TODO: Allow passing in an option to ignore the symbol table
//
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
#include "llvm/Bytecode/Reader.h"
#include "llvm/Bytecode/Format.h"
Module *C) {
// Clear out the local values table...
Values.clear();
- if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
+ if (MethodSignatureList.empty()) {
+ Error = "Method found, but MethodSignatureList empty!";
+ return failure(true); // Unexpected method!
+ }
const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
const MethodType *MTy = dyn_cast<const MethodType>(PMTy->getValueType());
for (MethodType::ParamTypes::const_iterator It = Params.begin();
It != Params.end(); ++It) {
MethodArgument *MA = new MethodArgument(*It);
- if (insertValue(MA, Values) == -1) { delete M; return failure(true); }
+ if (insertValue(MA, Values) == -1) {
+ Error = "Error reading method arguments!\n";
+ delete M; return failure(true);
+ }
M->getArgumentList().push_back(MA);
}
while (Buf < EndBuf) {
unsigned Type, Size;
const uchar *OldBuf = Buf;
- if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
+ if (readBlock(Buf, EndBuf, Type, Size)) {
+ Error = "Error reading Method level block!";
+ delete M; return failure(true);
+ }
switch (Type) {
case BytecodeFormat::ConstantPool:
BCR_TRACE(2, "} end block\n");
if (align32(Buf, EndBuf)) {
+ Error = "Error aligning Method level block!";
delete M; // Malformed bc file, read past end of block.
return failure(true);
}
if (postResolveValues(LateResolveValues) ||
postResolveValues(LateResolveModuleValues)) {
+ Error = "Error resolving method values!";
delete M; return failure(true); // Unresolvable references!
}
bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Module *Mod) {
- if (!MethodSignatureList.empty())
+ if (!MethodSignatureList.empty()) {
+ Error = "Two ModuleGlobalInfo packets found!";
return failure(true); // Two ModuleGlobal blocks?
+ }
// Read global variables...
unsigned VarType;
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
const Type *Ty = getType(VarType >> 2);
if (!Ty || !Ty->isPointerType()) {
- cerr << "Global not pointer type! Ty = " << Ty << endl;
+ Error = "Global not pointer type! Ty = " + Ty->getDescription();
return failure(true);
}
const Type *Ty = getType(MethSignature);
if (!Ty || !isa<PointerType>(Ty) ||
!isa<MethodType>(cast<PointerType>(Ty)->getValueType())) {
- cerr << "Method not ptr to meth type! Ty = " << Ty << endl;
+ Error = "Method not ptr to meth type! Ty = " + Ty->getDescription();
return failure(true);
}
unsigned Type, Size;
if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
- if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
+ if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
+ Error = "Expected Module packet!";
return failure(true); // Hrm, not a class?
+ }
BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
MethodSignatureList.clear(); // Just in case...
break;
default:
- cerr << " Unknown class block: " << Type << endl;
+ Error = "Expected Module Block!";
Buf += Size;
if (OldBuf > Buf) return failure(true); // Wrap around!
break;
if (align32(Buf, EndBuf)) { delete C; return failure(true); }
}
- if (!MethodSignatureList.empty()) // Expected more methods!
+ if (!MethodSignatureList.empty()) { // Expected more methods!
+ Error = "Method expected, but bytecode stream at end!";
return failure(true);
+ }
BCR_TRACE(0, "} end block\n\n");
return false;
unsigned Sig;
// Read and check signature...
if (read(Buf, EndBuf, Sig) ||
- Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
+ Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
+ Error = "Invalid bytecode signature!";
return failure<Module*>(0); // Invalid signature!
+ }
Module *Result;
if (ParseModule(Buf, EndBuf, Result)) return 0;
// Parse and return a class file...
//
-Module *ParseBytecodeFile(const string &Filename) {
+Module *ParseBytecodeFile(const string &Filename, string *ErrorStr) {
struct stat StatBuf;
Module *Result = 0;
if (Filename != string("-")) { // Read from a file...
int FD = open(Filename.c_str(), O_RDONLY);
- if (FD == -1) return failure<Module*>(0);
+ if (FD == -1) {
+ if (ErrorStr) *ErrorStr = "Error opening file!";
+ return failure<Module*>(0);
+ }
if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
int Length = StatBuf.st_size;
- if (Length == 0) { close(FD); return failure<Module*>(0); }
+ if (Length == 0) {
+ if (ErrorStr) *ErrorStr = "Error stat'ing file!";
+ close(FD); return failure<Module*>(0);
+ }
uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
MAP_PRIVATE, FD, 0);
- if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
+ if (Buffer == (uchar*)-1) {
+ if (ErrorStr) *ErrorStr = "Error mmapping file!";
+ close(FD); return failure<Module*>(0);
+ }
BytecodeParser Parser;
Result = Parser.ParseBytecode(Buffer, Buffer+Length);
munmap((char*)Buffer, Length);
close(FD);
+ if (ErrorStr) *ErrorStr = Parser.getError();
} else { // Read from stdin
size_t FileSize = 0;
int BlockSize;
FileSize += BlockSize;
}
- if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
+ if (FileSize == 0) {
+ if (ErrorStr) *ErrorStr = "Standard Input empty!";
+ free(FileData); return failure<Module*>(0);
+ }
#define ALIGN_PTRS 1
#if ALIGN_PTRS
#else
free(FileData); // Free realloc'd block of memory
#endif
+
+ if (ErrorStr) *ErrorStr = Parser.getError();
}
return Result;