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/unistd.h"
25 //===----------------------------------------------------------------------===//
26 // BytecodeFileReader - Read from an mmap'able file descriptor.
30 /// BytecodeFileReader - parses a bytecode file from a file
32 class BytecodeFileReader : public BytecodeParser {
34 unsigned char *Buffer;
37 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
38 void operator=(const BytecodeFileReader &BFR); // Do not implement
41 BytecodeFileReader(const std::string &Filename);
42 ~BytecodeFileReader();
46 static std::string ErrnoMessage (int savedErrNum, std::string descr) {
47 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
50 BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
51 Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
53 throw "Error reading file '" + Filename + "'.";
56 // Parse the bytecode we mmapped in
57 ParseBytecode(Buffer, Length, Filename);
59 UnmapFileFromAddressSpace(Buffer, Length);
64 BytecodeFileReader::~BytecodeFileReader() {
65 // Unmmap the bytecode...
66 UnmapFileFromAddressSpace(Buffer, Length);
69 //===----------------------------------------------------------------------===//
70 // BytecodeBufferReader - Read from a memory buffer
74 /// BytecodeBufferReader - parses a bytecode file from a buffer
76 class BytecodeBufferReader : public BytecodeParser {
78 const unsigned char *Buffer;
81 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
82 void operator=(const BytecodeBufferReader &BFR); // Do not implement
85 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
86 const std::string &ModuleID);
87 ~BytecodeBufferReader();
92 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
94 const std::string &ModuleID)
96 // If not aligned, allocate a new buffer to hold the bytecode...
97 const unsigned char *ParseBegin = 0;
98 if ((intptr_t)Buf & 3) {
99 Buffer = new unsigned char[Length+4];
100 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
101 ParseBegin = Buffer + Offset;
102 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
105 // If we don't need to copy it over, just use the caller's copy
106 ParseBegin = Buffer = Buf;
110 ParseBytecode(ParseBegin, Length, ModuleID);
112 if (MustDelete) delete [] Buffer;
117 BytecodeBufferReader::~BytecodeBufferReader() {
118 if (MustDelete) delete [] Buffer;
121 //===----------------------------------------------------------------------===//
122 // BytecodeStdinReader - Read bytecode from Standard Input
126 /// BytecodeStdinReader - parses a bytecode file from stdin
128 class BytecodeStdinReader : public BytecodeParser {
130 std::vector<unsigned char> FileData;
131 unsigned char *FileBuf;
133 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
134 void operator=(const BytecodeStdinReader &BFR); // Do not implement
137 BytecodeStdinReader();
141 BytecodeStdinReader::BytecodeStdinReader() {
143 unsigned char Buffer[4096*4];
145 // Read in all of the data from stdin, we cannot mmap stdin...
146 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
148 throw ErrnoMessage(errno, "read from standard input");
150 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
153 if (FileData.empty())
154 throw std::string("Standard Input empty!");
156 FileBuf = &FileData[0];
157 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
160 //===----------------------------------------------------------------------===//
161 // Varargs transmogrification code...
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();
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.
173 if (F->getFunctionType()->getNumParams() == 0)
174 return MP; // Modern varargs processing, just return.
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();
180 // If the user is making use of obsolete varargs intrinsics, adjust them for
182 if (Function *F = M->getNamedFunction("llvm.va_start")) {
183 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
185 const Type *RetTy = F->getFunctionType()->getParamType(0);
186 RetTy = cast<PointerType>(RetTy)->getElementType();
187 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
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);
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,
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);
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,
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);
232 //===----------------------------------------------------------------------===//
234 //===----------------------------------------------------------------------===//
236 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
239 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
241 const std::string &ModuleID) {
242 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
245 /// ParseBytecodeBuffer - Parse a given bytecode buffer
247 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
248 const std::string &ModuleID,
249 std::string *ErrorStr){
251 std::auto_ptr<ModuleProvider>
252 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
253 return AMP->releaseModule();
254 } catch (std::string &err) {
255 if (ErrorStr) *ErrorStr = err;
260 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
262 ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
263 if (Filename != std::string("-")) // Read from a file...
264 return CheckVarargs(new BytecodeFileReader(Filename));
265 else // Read from stdin
266 return CheckVarargs(new BytecodeStdinReader());
269 /// ParseBytecodeFile - Parse the given bytecode file
271 Module *llvm::ParseBytecodeFile(const std::string &Filename,
272 std::string *ErrorStr) {
274 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
275 return AMP->releaseModule();
276 } catch (std::string &err) {
277 if (ErrorStr) *ErrorStr = err;