blob: 291ad87344363eb815c5ce93075ae607891d76e4 [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"
Misha Brukman12c29d12003-09-22 23:38:23 +000019#include "Support/StringExtras.h"
20#include "Config/fcntl.h"
Brian Gaeke378b5242003-10-06 03:30:28 +000021#include <sys/stat.h>
Brian Gaeke27b40bc2003-12-12 00:47:44 +000022#include <cerrno>
Misha Brukman12c29d12003-09-22 23:38:23 +000023#include "Config/unistd.h"
24#include "Config/sys/mman.h"
Chris Lattnerdeab9a72003-11-19 16:06:55 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnercb7e2e22003-10-18 05:54:18 +000027//===----------------------------------------------------------------------===//
28// BytecodeFileReader - Read from an mmap'able file descriptor.
29//
30
Misha Brukman12c29d12003-09-22 23:38:23 +000031namespace {
Misha Brukmand57308a2003-09-23 16:13:28 +000032 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
33 /// when the object is destroyed.
34 ///
35 class FDHandle {
36 int FD;
37 public:
38 FDHandle(int fd) : FD(fd) {}
39 operator int() const { return FD; }
40 ~FDHandle() {
41 if (FD != -1) close(FD);
42 }
43 };
Misha Brukman12c29d12003-09-22 23:38:23 +000044
45 /// BytecodeFileReader - parses a bytecode file from a file
46 ///
47 class BytecodeFileReader : public BytecodeParser {
48 private:
49 unsigned char *Buffer;
50 int Length;
51
52 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000053 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000054
55 public:
56 BytecodeFileReader(const std::string &Filename);
57 ~BytecodeFileReader();
Misha Brukman12c29d12003-09-22 23:38:23 +000058 };
Misha Brukman12c29d12003-09-22 23:38:23 +000059}
60
Brian Gaeke27b40bc2003-12-12 00:47:44 +000061static std::string ErrnoMessage (int savedErrNum, std::string descr) {
62 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
63}
64
Misha Brukman12c29d12003-09-22 23:38:23 +000065BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
66 FDHandle FD = open(Filename.c_str(), O_RDONLY);
67 if (FD == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000068 throw ErrnoMessage(errno, "open '" + Filename + "'");
Misha Brukman12c29d12003-09-22 23:38:23 +000069
70 // Stat the file to get its length...
71 struct stat StatBuf;
72 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000073 throw ErrnoMessage(errno, "stat '" + Filename + "'");
Misha Brukman12c29d12003-09-22 23:38:23 +000074
75 // mmap in the file all at once...
76 Length = StatBuf.st_size;
Chris Lattner735289c2003-09-25 04:13:53 +000077 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
78
Misha Brukman12c29d12003-09-22 23:38:23 +000079 if (Buffer == (unsigned char*)MAP_FAILED)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000080 throw ErrnoMessage(errno, "map '" + Filename + "' into memory");
Misha Brukman12c29d12003-09-22 23:38:23 +000081
Misha Brukman7f58de22003-10-08 19:55:47 +000082 try {
83 // Parse the bytecode we mmapped in
84 ParseBytecode(Buffer, Length, Filename);
85 } catch (...) {
86 munmap((char*)Buffer, Length);
87 throw;
88 }
Misha Brukman12c29d12003-09-22 23:38:23 +000089}
90
91BytecodeFileReader::~BytecodeFileReader() {
92 // Unmmap the bytecode...
93 munmap((char*)Buffer, Length);
94}
95
Chris Lattnercb7e2e22003-10-18 05:54:18 +000096//===----------------------------------------------------------------------===//
97// BytecodeBufferReader - Read from a memory buffer
98//
Misha Brukmand57308a2003-09-23 16:13:28 +000099
100namespace {
101 /// BytecodeBufferReader - parses a bytecode file from a buffer
102 ///
103 class BytecodeBufferReader : public BytecodeParser {
104 private:
105 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +0000106 bool MustDelete;
107
108 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
109 void operator=(const BytecodeBufferReader &BFR); // Do not implement
110
111 public:
112 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
113 const std::string &ModuleID);
114 ~BytecodeBufferReader();
115
116 };
117}
118
119BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +0000120 unsigned Length,
Misha Brukmand57308a2003-09-23 16:13:28 +0000121 const std::string &ModuleID)
122{
123 // If not aligned, allocate a new buffer to hold the bytecode...
124 const unsigned char *ParseBegin = 0;
Misha Brukmand57308a2003-09-23 16:13:28 +0000125 if ((intptr_t)Buf & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000126 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000127 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000128 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000129 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000130 MustDelete = true;
131 } else {
132 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000133 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000134 MustDelete = false;
135 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000136 try {
137 ParseBytecode(ParseBegin, Length, ModuleID);
138 } catch (...) {
139 if (MustDelete) delete [] Buffer;
140 throw;
141 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000142}
143
144BytecodeBufferReader::~BytecodeBufferReader() {
145 if (MustDelete) delete [] Buffer;
146}
147
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000148//===----------------------------------------------------------------------===//
149// BytecodeStdinReader - Read bytecode from Standard Input
150//
Misha Brukmand57308a2003-09-23 16:13:28 +0000151
152namespace {
153 /// BytecodeStdinReader - parses a bytecode file from stdin
154 ///
155 class BytecodeStdinReader : public BytecodeParser {
156 private:
157 std::vector<unsigned char> FileData;
158 unsigned char *FileBuf;
159
160 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
161 void operator=(const BytecodeStdinReader &BFR); // Do not implement
162
163 public:
164 BytecodeStdinReader();
Misha Brukmand57308a2003-09-23 16:13:28 +0000165 };
166}
Misha Brukman12c29d12003-09-22 23:38:23 +0000167
Misha Brukman12c29d12003-09-22 23:38:23 +0000168BytecodeStdinReader::BytecodeStdinReader() {
169 int BlockSize;
170 unsigned char Buffer[4096*4];
171
172 // Read in all of the data from stdin, we cannot mmap stdin...
Brian Gaeked0fde302003-11-11 22:41:34 +0000173 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000174 if (BlockSize == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +0000175 throw ErrnoMessage(errno, "read from standard input");
Misha Brukmand57308a2003-09-23 16:13:28 +0000176
Misha Brukman12c29d12003-09-22 23:38:23 +0000177 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
178 }
179
180 if (FileData.empty())
181 throw std::string("Standard Input empty!");
182
Misha Brukman12c29d12003-09-22 23:38:23 +0000183 FileBuf = &FileData[0];
Misha Brukman12c29d12003-09-22 23:38:23 +0000184 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
185}
186
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000187//===----------------------------------------------------------------------===//
188// Varargs transmogrification code...
Misha Brukmand57308a2003-09-23 16:13:28 +0000189//
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000190
191// CheckVarargs - This is used to automatically translate old-style varargs to
192// new style varargs for backwards compatibility.
193static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
194 Module *M = MP->getModule();
195
196 // Check to see if va_start takes arguments...
197 Function *F = M->getNamedFunction("llvm.va_start");
198 if (F == 0) return MP; // No varargs use, just return.
199
200 if (F->getFunctionType()->getNumParams() == 0)
201 return MP; // Modern varargs processing, just return.
202
203 // If we get to this point, we know that we have an old-style module.
204 // Materialize the whole thing to perform the rewriting.
205 MP->materializeModule();
206
207 // If the user is making use of obsolete varargs intrinsics, adjust them for
208 // the user.
209 if (Function *F = M->getNamedFunction("llvm.va_start")) {
210 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
211
212 const Type *RetTy = F->getFunctionType()->getParamType(0);
213 RetTy = cast<PointerType>(RetTy)->getElementType();
214 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
215
216 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
217 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
218 Value *V = new CallInst(NF, "", CI);
219 new StoreInst(V, CI->getOperand(1), CI);
220 CI->getParent()->getInstList().erase(CI);
221 }
222 F->setName("");
223 }
224
225 if (Function *F = M->getNamedFunction("llvm.va_end")) {
226 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
227 const Type *ArgTy = F->getFunctionType()->getParamType(0);
228 ArgTy = cast<PointerType>(ArgTy)->getElementType();
229 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
230 ArgTy, 0);
231
232 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
233 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
234 Value *V = new LoadInst(CI->getOperand(1), "", CI);
235 new CallInst(NF, V, "", CI);
236 CI->getParent()->getInstList().erase(CI);
237 }
238 F->setName("");
239 }
240
241 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
242 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
243 const Type *ArgTy = F->getFunctionType()->getParamType(0);
244 ArgTy = cast<PointerType>(ArgTy)->getElementType();
245 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
246 ArgTy, 0);
247
248 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
249 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
250 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
251 new StoreInst(V, CI->getOperand(1), CI);
252 CI->getParent()->getInstList().erase(CI);
253 }
254 F->setName("");
255 }
256 return MP;
257}
258
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000259//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000260// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000261//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000262
263/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
264/// buffer
Chris Lattner00413e32003-10-04 20:14:59 +0000265ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000266llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
267 unsigned Length,
268 const std::string &ModuleID) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000269 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
Misha Brukman12c29d12003-09-22 23:38:23 +0000270}
271
Misha Brukmand57308a2003-09-23 16:13:28 +0000272/// ParseBytecodeBuffer - Parse a given bytecode buffer
273///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000274Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
275 const std::string &ModuleID,
276 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000277 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000278 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000279 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
280 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000281 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000282 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000283 return 0;
284 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000285}
286
Misha Brukmand57308a2003-09-23 16:13:28 +0000287/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000288///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000289ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000290 if (Filename != std::string("-")) // Read from a file...
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000291 return CheckVarargs(new BytecodeFileReader(Filename));
Misha Brukman12c29d12003-09-22 23:38:23 +0000292 else // Read from stdin
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000293 return CheckVarargs(new BytecodeStdinReader());
Misha Brukman12c29d12003-09-22 23:38:23 +0000294}
295
Misha Brukmand57308a2003-09-23 16:13:28 +0000296/// ParseBytecodeFile - Parse the given bytecode file
297///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000298Module *llvm::ParseBytecodeFile(const std::string &Filename,
299 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000300 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000301 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000302 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000303 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000304 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000305 return 0;
306 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000307}
Brian Gaeked0fde302003-11-11 22:41:34 +0000308