1 //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
3 // This file implements loading and parsing a bytecode file and parsing a
4 // bytecode module from a given buffer.
6 //===----------------------------------------------------------------------===//
8 #include "ReaderInternals.h"
9 #include "llvm/Module.h"
10 #include "llvm/Instructions.h"
11 #include "Support/StringExtras.h"
12 #include "Config/fcntl.h"
14 #include "Config/unistd.h"
15 #include "Config/sys/mman.h"
17 //===----------------------------------------------------------------------===//
18 // BytecodeFileReader - Read from an mmap'able file descriptor.
22 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
23 /// when the object is destroyed.
28 FDHandle(int fd) : FD(fd) {}
29 operator int() const { return FD; }
31 if (FD != -1) close(FD);
35 /// BytecodeFileReader - parses a bytecode file from a file
37 class BytecodeFileReader : public BytecodeParser {
39 unsigned char *Buffer;
42 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
43 void operator=(const BytecodeFileReader &BFR); // Do not implement
46 BytecodeFileReader(const std::string &Filename);
47 ~BytecodeFileReader();
51 BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
52 FDHandle FD = open(Filename.c_str(), O_RDONLY);
54 throw std::string("Error opening file!");
56 // Stat the file to get its length...
58 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
59 throw std::string("Error stat'ing file!");
61 // mmap in the file all at once...
62 Length = StatBuf.st_size;
63 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
65 if (Buffer == (unsigned char*)MAP_FAILED)
66 throw std::string("Error mmapping file!");
69 // Parse the bytecode we mmapped in
70 ParseBytecode(Buffer, Length, Filename);
72 munmap((char*)Buffer, Length);
77 BytecodeFileReader::~BytecodeFileReader() {
78 // Unmmap the bytecode...
79 munmap((char*)Buffer, Length);
82 //===----------------------------------------------------------------------===//
83 // BytecodeBufferReader - Read from a memory buffer
87 /// BytecodeBufferReader - parses a bytecode file from a buffer
89 class BytecodeBufferReader : public BytecodeParser {
91 const unsigned char *Buffer;
94 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
95 void operator=(const BytecodeBufferReader &BFR); // Do not implement
98 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
99 const std::string &ModuleID);
100 ~BytecodeBufferReader();
105 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
107 const std::string &ModuleID)
109 // If not aligned, allocate a new buffer to hold the bytecode...
110 const unsigned char *ParseBegin = 0;
111 if ((intptr_t)Buf & 3) {
112 Buffer = new unsigned char[Length+4];
113 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
114 ParseBegin = Buffer + Offset;
115 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
118 // If we don't need to copy it over, just use the caller's copy
119 ParseBegin = Buffer = Buf;
123 ParseBytecode(ParseBegin, Length, ModuleID);
125 if (MustDelete) delete [] Buffer;
130 BytecodeBufferReader::~BytecodeBufferReader() {
131 if (MustDelete) delete [] Buffer;
134 //===----------------------------------------------------------------------===//
135 // BytecodeStdinReader - Read bytecode from Standard Input
139 /// BytecodeStdinReader - parses a bytecode file from stdin
141 class BytecodeStdinReader : public BytecodeParser {
143 std::vector<unsigned char> FileData;
144 unsigned char *FileBuf;
146 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
147 void operator=(const BytecodeStdinReader &BFR); // Do not implement
150 BytecodeStdinReader();
154 BytecodeStdinReader::BytecodeStdinReader() {
156 unsigned char Buffer[4096*4];
158 // Read in all of the data from stdin, we cannot mmap stdin...
159 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
161 throw std::string("Error reading from stdin!");
163 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
166 if (FileData.empty())
167 throw std::string("Standard Input empty!");
169 FileBuf = &FileData[0];
170 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
173 //===----------------------------------------------------------------------===//
174 // Varargs transmogrification code...
177 // CheckVarargs - This is used to automatically translate old-style varargs to
178 // new style varargs for backwards compatibility.
179 static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
180 Module *M = MP->getModule();
182 // Check to see if va_start takes arguments...
183 Function *F = M->getNamedFunction("llvm.va_start");
184 if (F == 0) return MP; // No varargs use, just return.
186 if (F->getFunctionType()->getNumParams() == 0)
187 return MP; // Modern varargs processing, just return.
189 // If we get to this point, we know that we have an old-style module.
190 // Materialize the whole thing to perform the rewriting.
191 MP->materializeModule();
193 // If the user is making use of obsolete varargs intrinsics, adjust them for
195 if (Function *F = M->getNamedFunction("llvm.va_start")) {
196 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
198 const Type *RetTy = F->getFunctionType()->getParamType(0);
199 RetTy = cast<PointerType>(RetTy)->getElementType();
200 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
202 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
203 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
204 Value *V = new CallInst(NF, "", CI);
205 new StoreInst(V, CI->getOperand(1), CI);
206 CI->getParent()->getInstList().erase(CI);
211 if (Function *F = M->getNamedFunction("llvm.va_end")) {
212 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
213 const Type *ArgTy = F->getFunctionType()->getParamType(0);
214 ArgTy = cast<PointerType>(ArgTy)->getElementType();
215 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
218 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
219 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
220 Value *V = new LoadInst(CI->getOperand(1), "", CI);
221 new CallInst(NF, V, "", CI);
222 CI->getParent()->getInstList().erase(CI);
227 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
228 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
229 const Type *ArgTy = F->getFunctionType()->getParamType(0);
230 ArgTy = cast<PointerType>(ArgTy)->getElementType();
231 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
234 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
235 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
236 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
237 new StoreInst(V, CI->getOperand(1), CI);
238 CI->getParent()->getInstList().erase(CI);
246 //===----------------------------------------------------------------------===//
248 //===----------------------------------------------------------------------===//
250 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
253 getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length,
254 const std::string &ModuleID) {
255 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
258 /// ParseBytecodeBuffer - Parse a given bytecode buffer
260 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
261 const std::string &ModuleID, 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 *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 *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
285 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
286 return AMP->releaseModule();
287 } catch (std::string &err) {
288 if (ErrorStr) *ErrorStr = err;