Pull common code out

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5712 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-03-06 16:50:32 +00:00
parent fade83f62f
commit 09abe6aba3

View File

@ -581,15 +581,17 @@ Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
} }
Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length) { Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
std::string *ErrorStr) {
BytecodeParser Parser; BytecodeParser Parser;
return Parser.ParseBytecode(Buffer, Buffer+Length); Module *R = Parser.ParseBytecode(Buffer, Buffer+Length);
if (ErrorStr) *ErrorStr = Parser.getError();
return R;
} }
// Parse and return a class file... // Parse and return a class file...
// //
Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) { Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
struct stat StatBuf;
Module *Result = 0; Module *Result = 0;
if (Filename != std::string("-")) { // Read from a file... if (Filename != std::string("-")) { // Read from a file...
@ -599,26 +601,25 @@ Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
return 0; return 0;
} }
if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; } struct stat StatBuf;
if (fstat(FD, &StatBuf) == -1 ||
int Length = StatBuf.st_size; StatBuf.st_size == 0) {
if (Length == 0) {
if (ErrorStr) *ErrorStr = "Error stat'ing file!"; if (ErrorStr) *ErrorStr = "Error stat'ing file!";
close(FD); return 0; close(FD); return 0;
} }
uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
MAP_PRIVATE, FD, 0); int Length = StatBuf.st_size;
unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
MAP_PRIVATE, FD, 0);
if (Buffer == (uchar*)-1) { if (Buffer == (uchar*)-1) {
if (ErrorStr) *ErrorStr = "Error mmapping file!"; if (ErrorStr) *ErrorStr = "Error mmapping file!";
close(FD); return 0; close(FD); return 0;
} }
BytecodeParser Parser; Result = ParseBytecodeBuffer(Buffer, Length, ErrorStr);
Result = Parser.ParseBytecode(Buffer, Buffer+Length);
munmap((char*)Buffer, Length); munmap((char*)Buffer, Length);
close(FD); close(FD);
if (ErrorStr) *ErrorStr = Parser.getError();
} else { // Read from stdin } else { // Read from stdin
size_t FileSize = 0; size_t FileSize = 0;
int BlockSize; int BlockSize;
@ -647,16 +648,13 @@ Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
uchar *Buf = FileData; uchar *Buf = FileData;
#endif #endif
BytecodeParser Parser; Result = ParseBytecodeBuffer(Buf, FileSize, ErrorStr);
Result = Parser.ParseBytecode(Buf, Buf+FileSize);
#if ALIGN_PTRS #if ALIGN_PTRS
munmap((char*)Buf, FileSize); // Free mmap'd data area munmap((char*)Buf, FileSize); // Free mmap'd data area
#else #else
free(FileData); // Free realloc'd block of memory free(FileData); // Free realloc'd block of memory
#endif #endif
if (ErrorStr) *ErrorStr = Parser.getError();
} }
return Result; return Result;