blob: ef5e70d42b2ff60d965e8b246c75c93a424c6bd0 [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 <cerrno>
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 Brukman12c29d12003-09-22 23:38:23 +000032 /// BytecodeFileReader - parses a bytecode file from a file
33 ///
34 class BytecodeFileReader : public BytecodeParser {
35 private:
36 unsigned char *Buffer;
37 int Length;
38
39 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000040 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000041
42 public:
43 BytecodeFileReader(const std::string &Filename);
44 ~BytecodeFileReader();
Misha Brukman12c29d12003-09-22 23:38:23 +000045 };
Misha Brukman12c29d12003-09-22 23:38:23 +000046}
47
Brian Gaeke27b40bc2003-12-12 00:47:44 +000048static std::string ErrnoMessage (int savedErrNum, std::string descr) {
49 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
50}
51
Misha Brukman12c29d12003-09-22 23:38:23 +000052BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
Chris Lattnerb70abe12003-12-30 07:40:35 +000053 Length = getFileSize(Filename);
54 if (Length == -1)
55 throw ErrnoMessage(errno, "stat '" + Filename + "'");
56
Chris Lattner2d6481c2003-12-29 21:35:05 +000057 FDHandle FD(open(Filename.c_str(), O_RDONLY));
Misha Brukman12c29d12003-09-22 23:38:23 +000058 if (FD == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000059 throw ErrnoMessage(errno, "open '" + Filename + "'");
Misha Brukman12c29d12003-09-22 23:38:23 +000060
Misha Brukman12c29d12003-09-22 23:38:23 +000061 // mmap in the file all at once...
Chris Lattner735289c2003-09-25 04:13:53 +000062 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
63
Misha Brukman12c29d12003-09-22 23:38:23 +000064 if (Buffer == (unsigned char*)MAP_FAILED)
Brian Gaeke27b40bc2003-12-12 00:47:44 +000065 throw ErrnoMessage(errno, "map '" + Filename + "' into memory");
Misha Brukman12c29d12003-09-22 23:38:23 +000066
Misha Brukman7f58de22003-10-08 19:55:47 +000067 try {
68 // Parse the bytecode we mmapped in
69 ParseBytecode(Buffer, Length, Filename);
70 } catch (...) {
71 munmap((char*)Buffer, Length);
72 throw;
73 }
Misha Brukman12c29d12003-09-22 23:38:23 +000074}
75
76BytecodeFileReader::~BytecodeFileReader() {
77 // Unmmap the bytecode...
78 munmap((char*)Buffer, Length);
79}
80
Chris Lattnercb7e2e22003-10-18 05:54:18 +000081//===----------------------------------------------------------------------===//
82// BytecodeBufferReader - Read from a memory buffer
83//
Misha Brukmand57308a2003-09-23 16:13:28 +000084
85namespace {
86 /// BytecodeBufferReader - parses a bytecode file from a buffer
87 ///
88 class BytecodeBufferReader : public BytecodeParser {
89 private:
90 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000091 bool MustDelete;
92
93 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
94 void operator=(const BytecodeBufferReader &BFR); // Do not implement
95
96 public:
97 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
98 const std::string &ModuleID);
99 ~BytecodeBufferReader();
100
101 };
102}
103
104BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +0000105 unsigned Length,
Misha Brukmand57308a2003-09-23 16:13:28 +0000106 const std::string &ModuleID)
107{
108 // If not aligned, allocate a new buffer to hold the bytecode...
109 const unsigned char *ParseBegin = 0;
Misha Brukmand57308a2003-09-23 16:13:28 +0000110 if ((intptr_t)Buf & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000111 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000112 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000113 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000114 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000115 MustDelete = true;
116 } else {
117 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000118 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000119 MustDelete = false;
120 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000121 try {
122 ParseBytecode(ParseBegin, Length, ModuleID);
123 } catch (...) {
124 if (MustDelete) delete [] Buffer;
125 throw;
126 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000127}
128
129BytecodeBufferReader::~BytecodeBufferReader() {
130 if (MustDelete) delete [] Buffer;
131}
132
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000133//===----------------------------------------------------------------------===//
134// BytecodeStdinReader - Read bytecode from Standard Input
135//
Misha Brukmand57308a2003-09-23 16:13:28 +0000136
137namespace {
138 /// BytecodeStdinReader - parses a bytecode file from stdin
139 ///
140 class BytecodeStdinReader : public BytecodeParser {
141 private:
142 std::vector<unsigned char> FileData;
143 unsigned char *FileBuf;
144
145 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
146 void operator=(const BytecodeStdinReader &BFR); // Do not implement
147
148 public:
149 BytecodeStdinReader();
Misha Brukmand57308a2003-09-23 16:13:28 +0000150 };
151}
Misha Brukman12c29d12003-09-22 23:38:23 +0000152
Misha Brukman12c29d12003-09-22 23:38:23 +0000153BytecodeStdinReader::BytecodeStdinReader() {
154 int BlockSize;
155 unsigned char Buffer[4096*4];
156
157 // Read in all of the data from stdin, we cannot mmap stdin...
Brian Gaeked0fde302003-11-11 22:41:34 +0000158 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000159 if (BlockSize == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +0000160 throw ErrnoMessage(errno, "read from standard input");
Misha Brukmand57308a2003-09-23 16:13:28 +0000161
Misha Brukman12c29d12003-09-22 23:38:23 +0000162 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
163 }
164
165 if (FileData.empty())
166 throw std::string("Standard Input empty!");
167
Misha Brukman12c29d12003-09-22 23:38:23 +0000168 FileBuf = &FileData[0];
Misha Brukman12c29d12003-09-22 23:38:23 +0000169 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
170}
171
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000172//===----------------------------------------------------------------------===//
173// Varargs transmogrification code...
Misha Brukmand57308a2003-09-23 16:13:28 +0000174//
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000175
176// CheckVarargs - This is used to automatically translate old-style varargs to
177// new style varargs for backwards compatibility.
178static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
179 Module *M = MP->getModule();
180
181 // Check to see if va_start takes arguments...
182 Function *F = M->getNamedFunction("llvm.va_start");
183 if (F == 0) return MP; // No varargs use, just return.
184
185 if (F->getFunctionType()->getNumParams() == 0)
186 return MP; // Modern varargs processing, just return.
187
188 // If we get to this point, we know that we have an old-style module.
189 // Materialize the whole thing to perform the rewriting.
190 MP->materializeModule();
191
192 // If the user is making use of obsolete varargs intrinsics, adjust them for
193 // the user.
194 if (Function *F = M->getNamedFunction("llvm.va_start")) {
195 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
196
197 const Type *RetTy = F->getFunctionType()->getParamType(0);
198 RetTy = cast<PointerType>(RetTy)->getElementType();
199 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
200
201 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
202 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
203 Value *V = new CallInst(NF, "", CI);
204 new StoreInst(V, CI->getOperand(1), CI);
205 CI->getParent()->getInstList().erase(CI);
206 }
207 F->setName("");
208 }
209
210 if (Function *F = M->getNamedFunction("llvm.va_end")) {
211 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
212 const Type *ArgTy = F->getFunctionType()->getParamType(0);
213 ArgTy = cast<PointerType>(ArgTy)->getElementType();
214 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
215 ArgTy, 0);
216
217 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
218 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
219 Value *V = new LoadInst(CI->getOperand(1), "", CI);
220 new CallInst(NF, V, "", CI);
221 CI->getParent()->getInstList().erase(CI);
222 }
223 F->setName("");
224 }
225
226 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
227 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
228 const Type *ArgTy = F->getFunctionType()->getParamType(0);
229 ArgTy = cast<PointerType>(ArgTy)->getElementType();
230 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
231 ArgTy, 0);
232
233 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
234 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
235 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
236 new StoreInst(V, CI->getOperand(1), CI);
237 CI->getParent()->getInstList().erase(CI);
238 }
239 F->setName("");
240 }
241 return MP;
242}
243
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000244//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000245// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000246//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000247
248/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
249/// buffer
Chris Lattner00413e32003-10-04 20:14:59 +0000250ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000251llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
252 unsigned Length,
253 const std::string &ModuleID) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000254 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
Misha Brukman12c29d12003-09-22 23:38:23 +0000255}
256
Misha Brukmand57308a2003-09-23 16:13:28 +0000257/// ParseBytecodeBuffer - Parse a given bytecode buffer
258///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000259Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
260 const std::string &ModuleID,
261 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000262 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000263 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000264 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
265 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000266 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000267 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000268 return 0;
269 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000270}
271
Misha Brukmand57308a2003-09-23 16:13:28 +0000272/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000273///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000274ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000275 if (Filename != std::string("-")) // Read from a file...
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000276 return CheckVarargs(new BytecodeFileReader(Filename));
Misha Brukman12c29d12003-09-22 23:38:23 +0000277 else // Read from stdin
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000278 return CheckVarargs(new BytecodeStdinReader());
Misha Brukman12c29d12003-09-22 23:38:23 +0000279}
280
Misha Brukmand57308a2003-09-23 16:13:28 +0000281/// ParseBytecodeFile - Parse the given bytecode file
282///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000283Module *llvm::ParseBytecodeFile(const std::string &Filename,
284 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000285 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000286 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000287 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000288 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000289 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000290 return 0;
291 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000292}
Brian Gaeked0fde302003-11-11 22:41:34 +0000293