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