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