Misha Brukman | 4645379 | 2003-09-22 23:44:46 +0000 | [diff] [blame] | 1 | //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===// |
| 2 | // |
| 3 | // This file implements loading and parsing a bytecode file and parsing a |
| 4 | // bytecode module from a given buffer. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 8 | #include "ReaderInternals.h" |
| 9 | #include "Support/StringExtras.h" |
| 10 | #include "Config/fcntl.h" |
Brian Gaeke | 378b524 | 2003-10-06 03:30:28 +0000 | [diff] [blame] | 11 | #include <sys/stat.h> |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 12 | #include "Config/unistd.h" |
| 13 | #include "Config/sys/mman.h" |
| 14 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 15 | namespace { |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 16 | /// FDHandle - Simple handle class to make sure a file descriptor gets closed |
| 17 | /// when the object is destroyed. |
| 18 | /// |
| 19 | class FDHandle { |
| 20 | int FD; |
| 21 | public: |
| 22 | FDHandle(int fd) : FD(fd) {} |
| 23 | operator int() const { return FD; } |
| 24 | ~FDHandle() { |
| 25 | if (FD != -1) close(FD); |
| 26 | } |
| 27 | }; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 28 | |
| 29 | /// BytecodeFileReader - parses a bytecode file from a file |
| 30 | /// |
| 31 | class BytecodeFileReader : public BytecodeParser { |
| 32 | private: |
| 33 | unsigned char *Buffer; |
| 34 | int Length; |
| 35 | |
| 36 | BytecodeFileReader(const BytecodeFileReader&); // Do not implement |
Misha Brukman | 5c34441 | 2003-09-23 15:09:26 +0000 | [diff] [blame] | 37 | void operator=(const BytecodeFileReader &BFR); // Do not implement |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
| 40 | BytecodeFileReader(const std::string &Filename); |
| 41 | ~BytecodeFileReader(); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 42 | }; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | BytecodeFileReader::BytecodeFileReader(const std::string &Filename) { |
| 46 | FDHandle FD = open(Filename.c_str(), O_RDONLY); |
| 47 | if (FD == -1) |
| 48 | throw std::string("Error opening file!"); |
| 49 | |
| 50 | // Stat the file to get its length... |
| 51 | struct stat StatBuf; |
| 52 | if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0) |
| 53 | throw std::string("Error stat'ing file!"); |
| 54 | |
| 55 | // mmap in the file all at once... |
| 56 | Length = StatBuf.st_size; |
Chris Lattner | 735289c | 2003-09-25 04:13:53 +0000 | [diff] [blame] | 57 | Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0); |
| 58 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 59 | if (Buffer == (unsigned char*)MAP_FAILED) |
| 60 | throw std::string("Error mmapping file!"); |
| 61 | |
| 62 | // Parse the bytecode we mmapped in |
| 63 | ParseBytecode(Buffer, Length, Filename); |
| 64 | } |
| 65 | |
| 66 | BytecodeFileReader::~BytecodeFileReader() { |
| 67 | // Unmmap the bytecode... |
| 68 | munmap((char*)Buffer, Length); |
| 69 | } |
| 70 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 71 | //////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | namespace { |
| 74 | /// BytecodeBufferReader - parses a bytecode file from a buffer |
| 75 | /// |
| 76 | class BytecodeBufferReader : public BytecodeParser { |
| 77 | private: |
| 78 | const unsigned char *Buffer; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 79 | bool MustDelete; |
| 80 | |
| 81 | BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement |
| 82 | void operator=(const BytecodeBufferReader &BFR); // Do not implement |
| 83 | |
| 84 | public: |
| 85 | BytecodeBufferReader(const unsigned char *Buf, unsigned Length, |
| 86 | const std::string &ModuleID); |
| 87 | ~BytecodeBufferReader(); |
| 88 | |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf, |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 93 | unsigned Length, |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 94 | const std::string &ModuleID) |
| 95 | { |
| 96 | // If not aligned, allocate a new buffer to hold the bytecode... |
| 97 | const unsigned char *ParseBegin = 0; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 98 | if ((intptr_t)Buf & 3) { |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 99 | Buffer = new unsigned char[Length+4]; |
Chris Lattner | 4eed793 | 2003-09-24 22:34:17 +0000 | [diff] [blame] | 100 | unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 101 | ParseBegin = Buffer + Offset; |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 102 | memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 103 | MustDelete = true; |
| 104 | } else { |
| 105 | // If we don't need to copy it over, just use the caller's copy |
John Criswell | 4dcbd5e | 2003-09-23 21:19:11 +0000 | [diff] [blame] | 106 | ParseBegin = Buffer = Buf; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 107 | MustDelete = false; |
| 108 | } |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 109 | ParseBytecode(ParseBegin, Length, ModuleID); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | BytecodeBufferReader::~BytecodeBufferReader() { |
| 113 | if (MustDelete) delete [] Buffer; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | namespace { |
| 118 | /// BytecodeStdinReader - parses a bytecode file from stdin |
| 119 | /// |
| 120 | class BytecodeStdinReader : public BytecodeParser { |
| 121 | private: |
| 122 | std::vector<unsigned char> FileData; |
| 123 | unsigned char *FileBuf; |
| 124 | |
| 125 | BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement |
| 126 | void operator=(const BytecodeStdinReader &BFR); // Do not implement |
| 127 | |
| 128 | public: |
| 129 | BytecodeStdinReader(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 130 | }; |
| 131 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 132 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 133 | BytecodeStdinReader::BytecodeStdinReader() { |
| 134 | int BlockSize; |
| 135 | unsigned char Buffer[4096*4]; |
| 136 | |
| 137 | // Read in all of the data from stdin, we cannot mmap stdin... |
| 138 | while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) { |
| 139 | if (BlockSize == -1) |
| 140 | throw std::string("Error reading from stdin!"); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 141 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 142 | FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); |
| 143 | } |
| 144 | |
| 145 | if (FileData.empty()) |
| 146 | throw std::string("Standard Input empty!"); |
| 147 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 148 | FileBuf = &FileData[0]; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 149 | ParseBytecode(FileBuf, FileData.size(), "<stdin>"); |
| 150 | } |
| 151 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 152 | ///////////////////////////////////////////////////////////////////////////// |
| 153 | // |
| 154 | // Wrapper functions |
| 155 | // |
| 156 | ///////////////////////////////////////////////////////////////////////////// |
| 157 | |
| 158 | /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a |
| 159 | /// buffer |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 160 | ModuleProvider* |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 161 | getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length, |
| 162 | const std::string &ModuleID) { |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 163 | return new BytecodeBufferReader(Buffer, Length, ModuleID); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 166 | /// ParseBytecodeBuffer - Parse a given bytecode buffer |
| 167 | /// |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 168 | Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length, |
| 169 | const std::string &ModuleID, std::string *ErrorStr){ |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 170 | try { |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 171 | std::auto_ptr<ModuleProvider> |
Chris Lattner | a983359 | 2003-10-04 19:19:37 +0000 | [diff] [blame] | 172 | AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID)); |
| 173 | return AMP->releaseModule(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 174 | } catch (std::string &err) { |
Misha Brukman | 134aba6 | 2003-09-24 22:10:47 +0000 | [diff] [blame] | 175 | if (ErrorStr) *ErrorStr = err; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 180 | /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 181 | /// |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 182 | ModuleProvider *getBytecodeModuleProvider(const std::string &Filename) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 183 | if (Filename != std::string("-")) // Read from a file... |
| 184 | return new BytecodeFileReader(Filename); |
| 185 | else // Read from stdin |
| 186 | return new BytecodeStdinReader(); |
| 187 | } |
| 188 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 189 | /// ParseBytecodeFile - Parse the given bytecode file |
| 190 | /// |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 191 | Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) { |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 192 | try { |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 193 | std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename)); |
Chris Lattner | a983359 | 2003-10-04 19:19:37 +0000 | [diff] [blame] | 194 | return AMP->releaseModule(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 195 | } catch (std::string &err) { |
Misha Brukman | 134aba6 | 2003-09-24 22:10:47 +0000 | [diff] [blame] | 196 | if (ErrorStr) *ErrorStr = err; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 199 | } |