blob: bc3cbbd330938765cf92e3d730517514f0949542 [file] [log] [blame]
Misha Brukman46453792003-09-22 23:44:46 +00001//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Brukman46453792003-09-22 23:44:46 +00009//
10// This file implements loading and parsing a bytecode file and parsing a
11// bytecode module from a given buffer.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerdeab9a72003-11-19 16:06:55 +000015#include "llvm/Bytecode/Reader.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000016#include "ReaderInternals.h"
Chris Lattnercb7e2e22003-10-18 05:54:18 +000017#include "llvm/Module.h"
18#include "llvm/Instructions.h"
Chris Lattner2d6481c2003-12-29 21:35:05 +000019#include "Support/FileUtilities.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000020#include "Support/StringExtras.h"
21#include "Config/fcntl.h"
22#include "Config/unistd.h"
23#include "Config/sys/mman.h"
Chris Lattner2d6481c2003-12-29 21:35:05 +000024#include <sys/stat.h>
25#include <cerrno>
Chris Lattnerdeab9a72003-11-19 16:06:55 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnercb7e2e22003-10-18 05:54:18 +000028//===----------------------------------------------------------------------===//
29// BytecodeFileReader - Read from an mmap'able file descriptor.
30//
31
Misha Brukman12c29d12003-09-22 23:38:23 +000032namespace {
Misha Brukman12c29d12003-09-22 23:38:23 +000033 /// BytecodeFileReader - parses a bytecode file from a file
34 ///
35 class BytecodeFileReader : public BytecodeParser {
36 private:
37 unsigned char *Buffer;
38 int Length;
39
40 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000041 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000042
43 public:
44 BytecodeFileReader(const std::string &Filename);
45 ~BytecodeFileReader();
Misha Brukman12c29d12003-09-22 23:38:23 +000046 };
Misha Brukman12c29d12003-09-22 23:38:23 +000047}
48
Brian Gaeke27b40bc2003-12-12 00:47:44 +000049static std::string ErrnoMessage (int savedErrNum, std::string descr) {
50 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
51}
52
Misha Brukman12c29d12003-09-22 23:38:23 +000053BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
Chris Lattner2d6481c2003-12-29 21:35:05 +000054 FDHandle FD(open(Filename.c_str(), O_RDONLY));
Misha Brukman12c29d12003-09-22 23:38:23 +000055 if (FD == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000056 throw ErrnoMessage(errno, "open '" + Filename + "'");
Misha Brukman12c29d12003-09-22 23:38:23 +000057
58 // Stat the file to get its length...
59 struct stat StatBuf;
60 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000061 throw ErrnoMessage(errno, "stat '" + Filename + "'");
Misha Brukman12c29d12003-09-22 23:38:23 +000062
63 // mmap in the file all at once...
64 Length = StatBuf.st_size;
Chris Lattner735289c2003-09-25 04:13:53 +000065 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
66
Misha Brukman12c29d12003-09-22 23:38:23 +000067 if (Buffer == (unsigned char*)MAP_FAILED)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000068 throw ErrnoMessage(errno, "map '" + Filename + "' into memory");
Misha Brukman12c29d12003-09-22 23:38:23 +000069
Misha Brukman7f58de22003-10-08 19:55:47 +000070 try {
71 // Parse the bytecode we mmapped in
72 ParseBytecode(Buffer, Length, Filename);
73 } catch (...) {
74 munmap((char*)Buffer, Length);
75 throw;
76 }
Misha Brukman12c29d12003-09-22 23:38:23 +000077}
78
79BytecodeFileReader::~BytecodeFileReader() {
80 // Unmmap the bytecode...
81 munmap((char*)Buffer, Length);
82}
83
Chris Lattnercb7e2e22003-10-18 05:54:18 +000084//===----------------------------------------------------------------------===//
85// BytecodeBufferReader - Read from a memory buffer
86//
Misha Brukmand57308a2003-09-23 16:13:28 +000087
88namespace {
89 /// BytecodeBufferReader - parses a bytecode file from a buffer
90 ///
91 class BytecodeBufferReader : public BytecodeParser {
92 private:
93 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000094 bool MustDelete;
95
96 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
97 void operator=(const BytecodeBufferReader &BFR); // Do not implement
98
99 public:
100 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
101 const std::string &ModuleID);
102 ~BytecodeBufferReader();
103
104 };
105}
106
107BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +0000108 unsigned Length,
Misha Brukmand57308a2003-09-23 16:13:28 +0000109 const std::string &ModuleID)
110{
111 // If not aligned, allocate a new buffer to hold the bytecode...
112 const unsigned char *ParseBegin = 0;
Misha Brukmand57308a2003-09-23 16:13:28 +0000113 if ((intptr_t)Buf & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000114 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000115 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000116 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000117 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000118 MustDelete = true;
119 } else {
120 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000121 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000122 MustDelete = false;
123 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000124 try {
125 ParseBytecode(ParseBegin, Length, ModuleID);
126 } catch (...) {
127 if (MustDelete) delete [] Buffer;
128 throw;
129 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000130}
131
132BytecodeBufferReader::~BytecodeBufferReader() {
133 if (MustDelete) delete [] Buffer;
134}
135
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000136//===----------------------------------------------------------------------===//
137// BytecodeStdinReader - Read bytecode from Standard Input
138//
Misha Brukmand57308a2003-09-23 16:13:28 +0000139
140namespace {
141 /// BytecodeStdinReader - parses a bytecode file from stdin
142 ///
143 class BytecodeStdinReader : public BytecodeParser {
144 private:
145 std::vector<unsigned char> FileData;
146 unsigned char *FileBuf;
147
148 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
149 void operator=(const BytecodeStdinReader &BFR); // Do not implement
150
151 public:
152 BytecodeStdinReader();
Misha Brukmand57308a2003-09-23 16:13:28 +0000153 };
154}
Misha Brukman12c29d12003-09-22 23:38:23 +0000155
Misha Brukman12c29d12003-09-22 23:38:23 +0000156BytecodeStdinReader::BytecodeStdinReader() {
157 int BlockSize;
158 unsigned char Buffer[4096*4];
159
160 // Read in all of the data from stdin, we cannot mmap stdin...
Brian Gaeked0fde302003-11-11 22:41:34 +0000161 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000162 if (BlockSize == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +0000163 throw ErrnoMessage(errno, "read from standard input");
Misha Brukmand57308a2003-09-23 16:13:28 +0000164
Misha Brukman12c29d12003-09-22 23:38:23 +0000165 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
166 }
167
168 if (FileData.empty())
169 throw std::string("Standard Input empty!");
170
Misha Brukman12c29d12003-09-22 23:38:23 +0000171 FileBuf = &FileData[0];
Misha Brukman12c29d12003-09-22 23:38:23 +0000172 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
173}
174
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000175//===----------------------------------------------------------------------===//
176// Varargs transmogrification code...
Misha Brukmand57308a2003-09-23 16:13:28 +0000177//
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000178
179// CheckVarargs - This is used to automatically translate old-style varargs to
180// new style varargs for backwards compatibility.
181static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
182 Module *M = MP->getModule();
183
184 // Check to see if va_start takes arguments...
185 Function *F = M->getNamedFunction("llvm.va_start");
186 if (F == 0) return MP; // No varargs use, just return.
187
188 if (F->getFunctionType()->getNumParams() == 0)
189 return MP; // Modern varargs processing, just return.
190
191 // If we get to this point, we know that we have an old-style module.
192 // Materialize the whole thing to perform the rewriting.
193 MP->materializeModule();
194
195 // If the user is making use of obsolete varargs intrinsics, adjust them for
196 // the user.
197 if (Function *F = M->getNamedFunction("llvm.va_start")) {
198 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
199
200 const Type *RetTy = F->getFunctionType()->getParamType(0);
201 RetTy = cast<PointerType>(RetTy)->getElementType();
202 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
203
204 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
205 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
206 Value *V = new CallInst(NF, "", CI);
207 new StoreInst(V, CI->getOperand(1), CI);
208 CI->getParent()->getInstList().erase(CI);
209 }
210 F->setName("");
211 }
212
213 if (Function *F = M->getNamedFunction("llvm.va_end")) {
214 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
215 const Type *ArgTy = F->getFunctionType()->getParamType(0);
216 ArgTy = cast<PointerType>(ArgTy)->getElementType();
217 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
218 ArgTy, 0);
219
220 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
221 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
222 Value *V = new LoadInst(CI->getOperand(1), "", CI);
223 new CallInst(NF, V, "", CI);
224 CI->getParent()->getInstList().erase(CI);
225 }
226 F->setName("");
227 }
228
229 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
230 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
231 const Type *ArgTy = F->getFunctionType()->getParamType(0);
232 ArgTy = cast<PointerType>(ArgTy)->getElementType();
233 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
234 ArgTy, 0);
235
236 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
237 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
238 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
239 new StoreInst(V, CI->getOperand(1), CI);
240 CI->getParent()->getInstList().erase(CI);
241 }
242 F->setName("");
243 }
244 return MP;
245}
246
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000247//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000248// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000249//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000250
251/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
252/// buffer
Chris Lattner00413e32003-10-04 20:14:59 +0000253ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000254llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
255 unsigned Length,
256 const std::string &ModuleID) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000257 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
Misha Brukman12c29d12003-09-22 23:38:23 +0000258}
259
Misha Brukmand57308a2003-09-23 16:13:28 +0000260/// ParseBytecodeBuffer - Parse a given bytecode buffer
261///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000262Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
263 const std::string &ModuleID,
264 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000265 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000266 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000267 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
268 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000269 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000270 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000271 return 0;
272 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000273}
274
Misha Brukmand57308a2003-09-23 16:13:28 +0000275/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000276///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000277ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000278 if (Filename != std::string("-")) // Read from a file...
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000279 return CheckVarargs(new BytecodeFileReader(Filename));
Misha Brukman12c29d12003-09-22 23:38:23 +0000280 else // Read from stdin
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000281 return CheckVarargs(new BytecodeStdinReader());
Misha Brukman12c29d12003-09-22 23:38:23 +0000282}
283
Misha Brukmand57308a2003-09-23 16:13:28 +0000284/// ParseBytecodeFile - Parse the given bytecode file
285///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000286Module *llvm::ParseBytecodeFile(const std::string &Filename,
287 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000288 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000289 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000290 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000291 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000292 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000293 return 0;
294 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000295}
Brian Gaeked0fde302003-11-11 22:41:34 +0000296