Misha Brukman | 4645379 | 2003-09-22 23:44:46 +0000 | [diff] [blame] | 1 | //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 4645379 | 2003-09-22 23:44:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements loading and parsing a bytecode file and parsing a |
| 11 | // bytecode module from a given buffer. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 15 | #include "ReaderInternals.h" |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Instructions.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 18 | #include "Support/StringExtras.h" |
| 19 | #include "Config/fcntl.h" |
Brian Gaeke | 378b524 | 2003-10-06 03:30:28 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 21 | #include "Config/unistd.h" |
| 22 | #include "Config/sys/mman.h" |
| 23 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // BytecodeFileReader - Read from an mmap'able file descriptor. |
| 26 | // |
| 27 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 28 | namespace { |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 29 | /// FDHandle - Simple handle class to make sure a file descriptor gets closed |
| 30 | /// when the object is destroyed. |
| 31 | /// |
| 32 | class FDHandle { |
| 33 | int FD; |
| 34 | public: |
| 35 | FDHandle(int fd) : FD(fd) {} |
| 36 | operator int() const { return FD; } |
| 37 | ~FDHandle() { |
| 38 | if (FD != -1) close(FD); |
| 39 | } |
| 40 | }; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 41 | |
| 42 | /// BytecodeFileReader - parses a bytecode file from a file |
| 43 | /// |
| 44 | class BytecodeFileReader : public BytecodeParser { |
| 45 | private: |
| 46 | unsigned char *Buffer; |
| 47 | int Length; |
| 48 | |
| 49 | BytecodeFileReader(const BytecodeFileReader&); // Do not implement |
Misha Brukman | 5c34441 | 2003-09-23 15:09:26 +0000 | [diff] [blame] | 50 | void operator=(const BytecodeFileReader &BFR); // Do not implement |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 51 | |
| 52 | public: |
| 53 | BytecodeFileReader(const std::string &Filename); |
| 54 | ~BytecodeFileReader(); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 55 | }; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | BytecodeFileReader::BytecodeFileReader(const std::string &Filename) { |
| 59 | FDHandle FD = open(Filename.c_str(), O_RDONLY); |
| 60 | if (FD == -1) |
| 61 | throw std::string("Error opening file!"); |
| 62 | |
| 63 | // Stat the file to get its length... |
| 64 | struct stat StatBuf; |
| 65 | if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0) |
| 66 | throw std::string("Error stat'ing file!"); |
| 67 | |
| 68 | // mmap in the file all at once... |
| 69 | Length = StatBuf.st_size; |
Chris Lattner | 735289c | 2003-09-25 04:13:53 +0000 | [diff] [blame] | 70 | Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0); |
| 71 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 72 | if (Buffer == (unsigned char*)MAP_FAILED) |
| 73 | throw std::string("Error mmapping file!"); |
| 74 | |
Misha Brukman | 7f58de2 | 2003-10-08 19:55:47 +0000 | [diff] [blame] | 75 | try { |
| 76 | // Parse the bytecode we mmapped in |
| 77 | ParseBytecode(Buffer, Length, Filename); |
| 78 | } catch (...) { |
| 79 | munmap((char*)Buffer, Length); |
| 80 | throw; |
| 81 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | BytecodeFileReader::~BytecodeFileReader() { |
| 85 | // Unmmap the bytecode... |
| 86 | munmap((char*)Buffer, Length); |
| 87 | } |
| 88 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // BytecodeBufferReader - Read from a memory buffer |
| 91 | // |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 92 | |
| 93 | namespace { |
| 94 | /// BytecodeBufferReader - parses a bytecode file from a buffer |
| 95 | /// |
| 96 | class BytecodeBufferReader : public BytecodeParser { |
| 97 | private: |
| 98 | const unsigned char *Buffer; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 99 | bool MustDelete; |
| 100 | |
| 101 | BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement |
| 102 | void operator=(const BytecodeBufferReader &BFR); // Do not implement |
| 103 | |
| 104 | public: |
| 105 | BytecodeBufferReader(const unsigned char *Buf, unsigned Length, |
| 106 | const std::string &ModuleID); |
| 107 | ~BytecodeBufferReader(); |
| 108 | |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf, |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 113 | unsigned Length, |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 114 | const std::string &ModuleID) |
| 115 | { |
| 116 | // If not aligned, allocate a new buffer to hold the bytecode... |
| 117 | const unsigned char *ParseBegin = 0; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 118 | if ((intptr_t)Buf & 3) { |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 119 | Buffer = new unsigned char[Length+4]; |
Chris Lattner | 4eed793 | 2003-09-24 22:34:17 +0000 | [diff] [blame] | 120 | unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 121 | ParseBegin = Buffer + Offset; |
Misha Brukman | 34ce14b | 2003-09-24 22:04:02 +0000 | [diff] [blame] | 122 | memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 123 | MustDelete = true; |
| 124 | } else { |
| 125 | // 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] | 126 | ParseBegin = Buffer = Buf; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 127 | MustDelete = false; |
| 128 | } |
Misha Brukman | 7f58de2 | 2003-10-08 19:55:47 +0000 | [diff] [blame] | 129 | try { |
| 130 | ParseBytecode(ParseBegin, Length, ModuleID); |
| 131 | } catch (...) { |
| 132 | if (MustDelete) delete [] Buffer; |
| 133 | throw; |
| 134 | } |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BytecodeBufferReader::~BytecodeBufferReader() { |
| 138 | if (MustDelete) delete [] Buffer; |
| 139 | } |
| 140 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
| 142 | // BytecodeStdinReader - Read bytecode from Standard Input |
| 143 | // |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 144 | |
| 145 | namespace { |
| 146 | /// BytecodeStdinReader - parses a bytecode file from stdin |
| 147 | /// |
| 148 | class BytecodeStdinReader : public BytecodeParser { |
| 149 | private: |
| 150 | std::vector<unsigned char> FileData; |
| 151 | unsigned char *FileBuf; |
| 152 | |
| 153 | BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement |
| 154 | void operator=(const BytecodeStdinReader &BFR); // Do not implement |
| 155 | |
| 156 | public: |
| 157 | BytecodeStdinReader(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 158 | }; |
| 159 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 160 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 161 | BytecodeStdinReader::BytecodeStdinReader() { |
| 162 | int BlockSize; |
| 163 | unsigned char Buffer[4096*4]; |
| 164 | |
| 165 | // Read in all of the data from stdin, we cannot mmap stdin... |
| 166 | while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) { |
| 167 | if (BlockSize == -1) |
| 168 | throw std::string("Error reading from stdin!"); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 169 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 170 | FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); |
| 171 | } |
| 172 | |
| 173 | if (FileData.empty()) |
| 174 | throw std::string("Standard Input empty!"); |
| 175 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 176 | FileBuf = &FileData[0]; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 177 | ParseBytecode(FileBuf, FileData.size(), "<stdin>"); |
| 178 | } |
| 179 | |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// |
| 181 | // Varargs transmogrification code... |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 182 | // |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 183 | |
| 184 | // CheckVarargs - This is used to automatically translate old-style varargs to |
| 185 | // new style varargs for backwards compatibility. |
| 186 | static ModuleProvider *CheckVarargs(ModuleProvider *MP) { |
| 187 | Module *M = MP->getModule(); |
| 188 | |
| 189 | // Check to see if va_start takes arguments... |
| 190 | Function *F = M->getNamedFunction("llvm.va_start"); |
| 191 | if (F == 0) return MP; // No varargs use, just return. |
| 192 | |
| 193 | if (F->getFunctionType()->getNumParams() == 0) |
| 194 | return MP; // Modern varargs processing, just return. |
| 195 | |
| 196 | // If we get to this point, we know that we have an old-style module. |
| 197 | // Materialize the whole thing to perform the rewriting. |
| 198 | MP->materializeModule(); |
| 199 | |
| 200 | // If the user is making use of obsolete varargs intrinsics, adjust them for |
| 201 | // the user. |
| 202 | if (Function *F = M->getNamedFunction("llvm.va_start")) { |
| 203 | assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!"); |
| 204 | |
| 205 | const Type *RetTy = F->getFunctionType()->getParamType(0); |
| 206 | RetTy = cast<PointerType>(RetTy)->getElementType(); |
| 207 | Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0); |
| 208 | |
| 209 | for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ) |
| 210 | if (CallInst *CI = dyn_cast<CallInst>(*I++)) { |
| 211 | Value *V = new CallInst(NF, "", CI); |
| 212 | new StoreInst(V, CI->getOperand(1), CI); |
| 213 | CI->getParent()->getInstList().erase(CI); |
| 214 | } |
| 215 | F->setName(""); |
| 216 | } |
| 217 | |
| 218 | if (Function *F = M->getNamedFunction("llvm.va_end")) { |
| 219 | assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!"); |
| 220 | const Type *ArgTy = F->getFunctionType()->getParamType(0); |
| 221 | ArgTy = cast<PointerType>(ArgTy)->getElementType(); |
| 222 | Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy, |
| 223 | ArgTy, 0); |
| 224 | |
| 225 | for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ) |
| 226 | if (CallInst *CI = dyn_cast<CallInst>(*I++)) { |
| 227 | Value *V = new LoadInst(CI->getOperand(1), "", CI); |
| 228 | new CallInst(NF, V, "", CI); |
| 229 | CI->getParent()->getInstList().erase(CI); |
| 230 | } |
| 231 | F->setName(""); |
| 232 | } |
| 233 | |
| 234 | if (Function *F = M->getNamedFunction("llvm.va_copy")) { |
| 235 | assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!"); |
| 236 | const Type *ArgTy = F->getFunctionType()->getParamType(0); |
| 237 | ArgTy = cast<PointerType>(ArgTy)->getElementType(); |
| 238 | Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy, |
| 239 | ArgTy, 0); |
| 240 | |
| 241 | for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ) |
| 242 | if (CallInst *CI = dyn_cast<CallInst>(*I++)) { |
| 243 | Value *V = new CallInst(NF, CI->getOperand(2), "", CI); |
| 244 | new StoreInst(V, CI->getOperand(1), CI); |
| 245 | CI->getParent()->getInstList().erase(CI); |
| 246 | } |
| 247 | F->setName(""); |
| 248 | } |
| 249 | return MP; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | //===----------------------------------------------------------------------===// |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 254 | // Wrapper functions |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 256 | |
| 257 | /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a |
| 258 | /// buffer |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 259 | ModuleProvider* |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 260 | getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length, |
| 261 | const std::string &ModuleID) { |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 262 | return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID)); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 265 | /// ParseBytecodeBuffer - Parse a given bytecode buffer |
| 266 | /// |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 267 | Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length, |
| 268 | const std::string &ModuleID, std::string *ErrorStr){ |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 269 | try { |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 270 | std::auto_ptr<ModuleProvider> |
Chris Lattner | a983359 | 2003-10-04 19:19:37 +0000 | [diff] [blame] | 271 | AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID)); |
| 272 | return AMP->releaseModule(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 273 | } catch (std::string &err) { |
Misha Brukman | 134aba6 | 2003-09-24 22:10:47 +0000 | [diff] [blame] | 274 | if (ErrorStr) *ErrorStr = err; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 279 | /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 280 | /// |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 281 | ModuleProvider *getBytecodeModuleProvider(const std::string &Filename) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 282 | if (Filename != std::string("-")) // Read from a file... |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 283 | return CheckVarargs(new BytecodeFileReader(Filename)); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 284 | else // Read from stdin |
Chris Lattner | cb7e2e2 | 2003-10-18 05:54:18 +0000 | [diff] [blame] | 285 | return CheckVarargs(new BytecodeStdinReader()); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 288 | /// ParseBytecodeFile - Parse the given bytecode file |
| 289 | /// |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 290 | Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) { |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 291 | try { |
Chris Lattner | 00413e3 | 2003-10-04 20:14:59 +0000 | [diff] [blame] | 292 | std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename)); |
Chris Lattner | a983359 | 2003-10-04 19:19:37 +0000 | [diff] [blame] | 293 | return AMP->releaseModule(); |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 294 | } catch (std::string &err) { |
Misha Brukman | 134aba6 | 2003-09-24 22:10:47 +0000 | [diff] [blame] | 295 | if (ErrorStr) *ErrorStr = err; |
Misha Brukman | d57308a | 2003-09-23 16:13:28 +0000 | [diff] [blame] | 296 | return 0; |
| 297 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 298 | } |