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