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 "ReaderInternals.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "Support/StringExtras.h"
19 #include "Config/fcntl.h"
21 #include "Config/unistd.h"
22 #include "Config/sys/mman.h"
24 //===----------------------------------------------------------------------===//
25 // BytecodeFileReader - Read from an mmap'able file descriptor.
29 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
30 /// when the object is destroyed.
35 FDHandle(int fd) : FD(fd) {}
36 operator int() const { return FD; }
38 if (FD != -1) close(FD);
42 /// BytecodeFileReader - parses a bytecode file from a file
44 class BytecodeFileReader : public BytecodeParser {
46 unsigned char *Buffer;
49 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
50 void operator=(const BytecodeFileReader &BFR); // Do not implement
53 BytecodeFileReader(const std::string &Filename);
54 ~BytecodeFileReader();
58 BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
59 FDHandle FD = open(Filename.c_str(), O_RDONLY);
61 throw std::string("Error opening file!");
63 // Stat the file to get its length...
65 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
66 throw std::string("Error stat'ing file!");
68 // mmap in the file all at once...
69 Length = StatBuf.st_size;
70 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
72 if (Buffer == (unsigned char*)MAP_FAILED)
73 throw std::string("Error mmapping file!");
76 // Parse the bytecode we mmapped in
77 ParseBytecode(Buffer, Length, Filename);
79 munmap((char*)Buffer, Length);
84 BytecodeFileReader::~BytecodeFileReader() {
85 // Unmmap the bytecode...
86 munmap((char*)Buffer, Length);
89 //===----------------------------------------------------------------------===//
90 // BytecodeBufferReader - Read from a memory buffer
94 /// BytecodeBufferReader - parses a bytecode file from a buffer
96 class BytecodeBufferReader : public BytecodeParser {
98 const unsigned char *Buffer;
101 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
102 void operator=(const BytecodeBufferReader &BFR); // Do not implement
105 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
106 const std::string &ModuleID);
107 ~BytecodeBufferReader();
112 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
114 const std::string &ModuleID)
116 // If not aligned, allocate a new buffer to hold the bytecode...
117 const unsigned char *ParseBegin = 0;
118 if ((intptr_t)Buf & 3) {
119 Buffer = new unsigned char[Length+4];
120 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
121 ParseBegin = Buffer + Offset;
122 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
125 // If we don't need to copy it over, just use the caller's copy
126 ParseBegin = Buffer = Buf;
130 ParseBytecode(ParseBegin, Length, ModuleID);
132 if (MustDelete) delete [] Buffer;
137 BytecodeBufferReader::~BytecodeBufferReader() {
138 if (MustDelete) delete [] Buffer;
141 //===----------------------------------------------------------------------===//
142 // BytecodeStdinReader - Read bytecode from Standard Input
146 /// BytecodeStdinReader - parses a bytecode file from stdin
148 class BytecodeStdinReader : public BytecodeParser {
150 std::vector<unsigned char> FileData;
151 unsigned char *FileBuf;
153 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
154 void operator=(const BytecodeStdinReader &BFR); // Do not implement
157 BytecodeStdinReader();
161 BytecodeStdinReader::BytecodeStdinReader() {
163 unsigned char Buffer[4096*4];
165 // Read in all of the data from stdin, we cannot mmap stdin...
166 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
168 throw std::string("Error reading from stdin!");
170 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
173 if (FileData.empty())
174 throw std::string("Standard Input empty!");
176 FileBuf = &FileData[0];
177 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
180 //===----------------------------------------------------------------------===//
181 // Varargs transmogrification code...
184 // CheckVarargs - This is used to automatically translate old-style varargs to
185 // new style varargs for backwards compatibility.
186 static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
187 Module *M = MP->getModule();
189 // Check to see if va_start takes arguments...
190 Function *F = M->getNamedFunction("llvm.va_start");
191 if (F == 0) return MP; // No varargs use, just return.
193 if (F->getFunctionType()->getNumParams() == 0)
194 return MP; // Modern varargs processing, just return.
196 // If we get to this point, we know that we have an old-style module.
197 // Materialize the whole thing to perform the rewriting.
198 MP->materializeModule();
200 // If the user is making use of obsolete varargs intrinsics, adjust them for
202 if (Function *F = M->getNamedFunction("llvm.va_start")) {
203 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
205 const Type *RetTy = F->getFunctionType()->getParamType(0);
206 RetTy = cast<PointerType>(RetTy)->getElementType();
207 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
209 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
210 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
211 Value *V = new CallInst(NF, "", CI);
212 new StoreInst(V, CI->getOperand(1), CI);
213 CI->getParent()->getInstList().erase(CI);
218 if (Function *F = M->getNamedFunction("llvm.va_end")) {
219 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
220 const Type *ArgTy = F->getFunctionType()->getParamType(0);
221 ArgTy = cast<PointerType>(ArgTy)->getElementType();
222 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
225 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
226 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
227 Value *V = new LoadInst(CI->getOperand(1), "", CI);
228 new CallInst(NF, V, "", CI);
229 CI->getParent()->getInstList().erase(CI);
234 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
235 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
236 const Type *ArgTy = F->getFunctionType()->getParamType(0);
237 ArgTy = cast<PointerType>(ArgTy)->getElementType();
238 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
241 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
242 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
243 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
244 new StoreInst(V, CI->getOperand(1), CI);
245 CI->getParent()->getInstList().erase(CI);
253 //===----------------------------------------------------------------------===//
255 //===----------------------------------------------------------------------===//
257 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
260 getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length,
261 const std::string &ModuleID) {
262 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
265 /// ParseBytecodeBuffer - Parse a given bytecode buffer
267 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
268 const std::string &ModuleID, std::string *ErrorStr){
270 std::auto_ptr<ModuleProvider>
271 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
272 return AMP->releaseModule();
273 } catch (std::string &err) {
274 if (ErrorStr) *ErrorStr = err;
279 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
281 ModuleProvider *getBytecodeModuleProvider(const std::string &Filename) {
282 if (Filename != std::string("-")) // Read from a file...
283 return CheckVarargs(new BytecodeFileReader(Filename));
284 else // Read from stdin
285 return CheckVarargs(new BytecodeStdinReader());
288 /// ParseBytecodeFile - Parse the given bytecode file
290 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
292 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
293 return AMP->releaseModule();
294 } catch (std::string &err) {
295 if (ErrorStr) *ErrorStr = err;