1 //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements loading and parsing a bytecode file and parsing a
11 // bytecode module from a given buffer.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bytecode/Reader.h"
16 #include "ReaderInternals.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "Support/FileUtilities.h"
20 #include "Support/StringExtras.h"
21 #include "Config/fcntl.h"
22 #include "Config/unistd.h"
23 #include "Config/sys/mman.h"
27 //===----------------------------------------------------------------------===//
28 // BytecodeFileReader - Read from an mmap'able file descriptor.
32 /// BytecodeFileReader - parses a bytecode file from a file
34 class BytecodeFileReader : public BytecodeParser {
36 unsigned char *Buffer;
39 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
40 void operator=(const BytecodeFileReader &BFR); // Do not implement
43 BytecodeFileReader(const std::string &Filename);
44 ~BytecodeFileReader();
48 static std::string ErrnoMessage (int savedErrNum, std::string descr) {
49 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
52 BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
53 Length = getFileSize(Filename);
55 throw ErrnoMessage(errno, "stat '" + Filename + "'");
57 FDHandle FD(open(Filename.c_str(), O_RDONLY));
59 throw ErrnoMessage(errno, "open '" + Filename + "'");
61 // mmap in the file all at once...
62 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
64 if (Buffer == (unsigned char*)MAP_FAILED)
65 throw ErrnoMessage(errno, "map '" + Filename + "' into memory");
68 // Parse the bytecode we mmapped in
69 ParseBytecode(Buffer, Length, Filename);
71 munmap((char*)Buffer, Length);
76 BytecodeFileReader::~BytecodeFileReader() {
77 // Unmmap the bytecode...
78 munmap((char*)Buffer, Length);
81 //===----------------------------------------------------------------------===//
82 // BytecodeBufferReader - Read from a memory buffer
86 /// BytecodeBufferReader - parses a bytecode file from a buffer
88 class BytecodeBufferReader : public BytecodeParser {
90 const unsigned char *Buffer;
93 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
94 void operator=(const BytecodeBufferReader &BFR); // Do not implement
97 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
98 const std::string &ModuleID);
99 ~BytecodeBufferReader();
104 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
106 const std::string &ModuleID)
108 // If not aligned, allocate a new buffer to hold the bytecode...
109 const unsigned char *ParseBegin = 0;
110 if ((intptr_t)Buf & 3) {
111 Buffer = new unsigned char[Length+4];
112 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
113 ParseBegin = Buffer + Offset;
114 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
117 // If we don't need to copy it over, just use the caller's copy
118 ParseBegin = Buffer = Buf;
122 ParseBytecode(ParseBegin, Length, ModuleID);
124 if (MustDelete) delete [] Buffer;
129 BytecodeBufferReader::~BytecodeBufferReader() {
130 if (MustDelete) delete [] Buffer;
133 //===----------------------------------------------------------------------===//
134 // BytecodeStdinReader - Read bytecode from Standard Input
138 /// BytecodeStdinReader - parses a bytecode file from stdin
140 class BytecodeStdinReader : public BytecodeParser {
142 std::vector<unsigned char> FileData;
143 unsigned char *FileBuf;
145 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
146 void operator=(const BytecodeStdinReader &BFR); // Do not implement
149 BytecodeStdinReader();
153 BytecodeStdinReader::BytecodeStdinReader() {
155 unsigned char Buffer[4096*4];
157 // Read in all of the data from stdin, we cannot mmap stdin...
158 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
160 throw ErrnoMessage(errno, "read from standard input");
162 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
165 if (FileData.empty())
166 throw std::string("Standard Input empty!");
168 FileBuf = &FileData[0];
169 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
172 //===----------------------------------------------------------------------===//
173 // Varargs transmogrification code...
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();
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.
185 if (F->getFunctionType()->getNumParams() == 0)
186 return MP; // Modern varargs processing, just return.
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();
192 // If the user is making use of obsolete varargs intrinsics, adjust them for
194 if (Function *F = M->getNamedFunction("llvm.va_start")) {
195 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
197 const Type *RetTy = F->getFunctionType()->getParamType(0);
198 RetTy = cast<PointerType>(RetTy)->getElementType();
199 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
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);
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,
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);
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,
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);
244 //===----------------------------------------------------------------------===//
246 //===----------------------------------------------------------------------===//
248 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
251 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
253 const std::string &ModuleID) {
254 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
257 /// ParseBytecodeBuffer - Parse a given bytecode buffer
259 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
260 const std::string &ModuleID,
261 std::string *ErrorStr){
263 std::auto_ptr<ModuleProvider>
264 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
265 return AMP->releaseModule();
266 } catch (std::string &err) {
267 if (ErrorStr) *ErrorStr = err;
272 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
274 ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
275 if (Filename != std::string("-")) // Read from a file...
276 return CheckVarargs(new BytecodeFileReader(Filename));
277 else // Read from stdin
278 return CheckVarargs(new BytecodeStdinReader());
281 /// ParseBytecodeFile - Parse the given bytecode file
283 Module *llvm::ParseBytecodeFile(const std::string &Filename,
284 std::string *ErrorStr) {
286 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
287 return AMP->releaseModule();
288 } catch (std::string &err) {
289 if (ErrorStr) *ErrorStr = err;