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