blob: 3de1dd442500b1a799e481236f9a4483828b4e02 [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>
Misha Brukman12c29d12003-09-22 23:38:23 +000022#include "Config/unistd.h"
23#include "Config/sys/mman.h"
Chris Lattnerdeab9a72003-11-19 16:06:55 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnercb7e2e22003-10-18 05:54:18 +000026//===----------------------------------------------------------------------===//
27// BytecodeFileReader - Read from an mmap'able file descriptor.
28//
29
Misha Brukman12c29d12003-09-22 23:38:23 +000030namespace {
Misha Brukmand57308a2003-09-23 16:13:28 +000031 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
32 /// when the object is destroyed.
33 ///
34 class FDHandle {
35 int FD;
36 public:
37 FDHandle(int fd) : FD(fd) {}
38 operator int() const { return FD; }
39 ~FDHandle() {
40 if (FD != -1) close(FD);
41 }
42 };
Misha Brukman12c29d12003-09-22 23:38:23 +000043
44 /// BytecodeFileReader - parses a bytecode file from a file
45 ///
46 class BytecodeFileReader : public BytecodeParser {
47 private:
48 unsigned char *Buffer;
49 int Length;
50
51 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000052 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000053
54 public:
55 BytecodeFileReader(const std::string &Filename);
56 ~BytecodeFileReader();
Misha Brukman12c29d12003-09-22 23:38:23 +000057 };
Misha Brukman12c29d12003-09-22 23:38:23 +000058}
59
60BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
61 FDHandle FD = open(Filename.c_str(), O_RDONLY);
62 if (FD == -1)
63 throw std::string("Error opening file!");
64
65 // Stat the file to get its length...
66 struct stat StatBuf;
67 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
68 throw std::string("Error stat'ing file!");
69
70 // mmap in the file all at once...
71 Length = StatBuf.st_size;
Chris Lattner735289c2003-09-25 04:13:53 +000072 Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
73
Misha Brukman12c29d12003-09-22 23:38:23 +000074 if (Buffer == (unsigned char*)MAP_FAILED)
75 throw std::string("Error mmapping file!");
76
Misha Brukman7f58de22003-10-08 19:55:47 +000077 try {
78 // Parse the bytecode we mmapped in
79 ParseBytecode(Buffer, Length, Filename);
80 } catch (...) {
81 munmap((char*)Buffer, Length);
82 throw;
83 }
Misha Brukman12c29d12003-09-22 23:38:23 +000084}
85
86BytecodeFileReader::~BytecodeFileReader() {
87 // Unmmap the bytecode...
88 munmap((char*)Buffer, Length);
89}
90
Chris Lattnercb7e2e22003-10-18 05:54:18 +000091//===----------------------------------------------------------------------===//
92// BytecodeBufferReader - Read from a memory buffer
93//
Misha Brukmand57308a2003-09-23 16:13:28 +000094
95namespace {
96 /// BytecodeBufferReader - parses a bytecode file from a buffer
97 ///
98 class BytecodeBufferReader : public BytecodeParser {
99 private:
100 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +0000101 bool MustDelete;
102
103 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
104 void operator=(const BytecodeBufferReader &BFR); // Do not implement
105
106 public:
107 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
108 const std::string &ModuleID);
109 ~BytecodeBufferReader();
110
111 };
112}
113
114BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +0000115 unsigned Length,
Misha Brukmand57308a2003-09-23 16:13:28 +0000116 const std::string &ModuleID)
117{
118 // If not aligned, allocate a new buffer to hold the bytecode...
119 const unsigned char *ParseBegin = 0;
Misha Brukmand57308a2003-09-23 16:13:28 +0000120 if ((intptr_t)Buf & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000121 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000122 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000123 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000124 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000125 MustDelete = true;
126 } else {
127 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000128 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000129 MustDelete = false;
130 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000131 try {
132 ParseBytecode(ParseBegin, Length, ModuleID);
133 } catch (...) {
134 if (MustDelete) delete [] Buffer;
135 throw;
136 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000137}
138
139BytecodeBufferReader::~BytecodeBufferReader() {
140 if (MustDelete) delete [] Buffer;
141}
142
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000143//===----------------------------------------------------------------------===//
144// BytecodeStdinReader - Read bytecode from Standard Input
145//
Misha Brukmand57308a2003-09-23 16:13:28 +0000146
147namespace {
148 /// BytecodeStdinReader - parses a bytecode file from stdin
149 ///
150 class BytecodeStdinReader : public BytecodeParser {
151 private:
152 std::vector<unsigned char> FileData;
153 unsigned char *FileBuf;
154
155 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
156 void operator=(const BytecodeStdinReader &BFR); // Do not implement
157
158 public:
159 BytecodeStdinReader();
Misha Brukmand57308a2003-09-23 16:13:28 +0000160 };
161}
Misha Brukman12c29d12003-09-22 23:38:23 +0000162
Misha Brukman12c29d12003-09-22 23:38:23 +0000163BytecodeStdinReader::BytecodeStdinReader() {
164 int BlockSize;
165 unsigned char Buffer[4096*4];
166
167 // Read in all of the data from stdin, we cannot mmap stdin...
Brian Gaeked0fde302003-11-11 22:41:34 +0000168 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000169 if (BlockSize == -1)
170 throw std::string("Error reading from stdin!");
Misha Brukmand57308a2003-09-23 16:13:28 +0000171
Misha Brukman12c29d12003-09-22 23:38:23 +0000172 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
173 }
174
175 if (FileData.empty())
176 throw std::string("Standard Input empty!");
177
Misha Brukman12c29d12003-09-22 23:38:23 +0000178 FileBuf = &FileData[0];
Misha Brukman12c29d12003-09-22 23:38:23 +0000179 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
180}
181
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000182//===----------------------------------------------------------------------===//
183// Varargs transmogrification code...
Misha Brukmand57308a2003-09-23 16:13:28 +0000184//
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000185
186// CheckVarargs - This is used to automatically translate old-style varargs to
187// new style varargs for backwards compatibility.
188static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
189 Module *M = MP->getModule();
190
191 // Check to see if va_start takes arguments...
192 Function *F = M->getNamedFunction("llvm.va_start");
193 if (F == 0) return MP; // No varargs use, just return.
194
195 if (F->getFunctionType()->getNumParams() == 0)
196 return MP; // Modern varargs processing, just return.
197
198 // If we get to this point, we know that we have an old-style module.
199 // Materialize the whole thing to perform the rewriting.
200 MP->materializeModule();
201
202 // If the user is making use of obsolete varargs intrinsics, adjust them for
203 // the user.
204 if (Function *F = M->getNamedFunction("llvm.va_start")) {
205 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
206
207 const Type *RetTy = F->getFunctionType()->getParamType(0);
208 RetTy = cast<PointerType>(RetTy)->getElementType();
209 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
210
211 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
212 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
213 Value *V = new CallInst(NF, "", CI);
214 new StoreInst(V, CI->getOperand(1), CI);
215 CI->getParent()->getInstList().erase(CI);
216 }
217 F->setName("");
218 }
219
220 if (Function *F = M->getNamedFunction("llvm.va_end")) {
221 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
222 const Type *ArgTy = F->getFunctionType()->getParamType(0);
223 ArgTy = cast<PointerType>(ArgTy)->getElementType();
224 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
225 ArgTy, 0);
226
227 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
228 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
229 Value *V = new LoadInst(CI->getOperand(1), "", CI);
230 new CallInst(NF, V, "", CI);
231 CI->getParent()->getInstList().erase(CI);
232 }
233 F->setName("");
234 }
235
236 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
237 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
238 const Type *ArgTy = F->getFunctionType()->getParamType(0);
239 ArgTy = cast<PointerType>(ArgTy)->getElementType();
240 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
241 ArgTy, 0);
242
243 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
244 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
245 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
246 new StoreInst(V, CI->getOperand(1), CI);
247 CI->getParent()->getInstList().erase(CI);
248 }
249 F->setName("");
250 }
251 return MP;
252}
253
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000254//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000255// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000256//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000257
258/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
259/// buffer
Chris Lattner00413e32003-10-04 20:14:59 +0000260ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000261llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
262 unsigned Length,
263 const std::string &ModuleID) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000264 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
Misha Brukman12c29d12003-09-22 23:38:23 +0000265}
266
Misha Brukmand57308a2003-09-23 16:13:28 +0000267/// ParseBytecodeBuffer - Parse a given bytecode buffer
268///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000269Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
270 const std::string &ModuleID,
271 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000272 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000273 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000274 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
275 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000276 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000277 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000278 return 0;
279 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000280}
281
Misha Brukmand57308a2003-09-23 16:13:28 +0000282/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000283///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000284ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000285 if (Filename != std::string("-")) // Read from a file...
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000286 return CheckVarargs(new BytecodeFileReader(Filename));
Misha Brukman12c29d12003-09-22 23:38:23 +0000287 else // Read from stdin
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000288 return CheckVarargs(new BytecodeStdinReader());
Misha Brukman12c29d12003-09-22 23:38:23 +0000289}
290
Misha Brukmand57308a2003-09-23 16:13:28 +0000291/// ParseBytecodeFile - Parse the given bytecode file
292///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000293Module *llvm::ParseBytecodeFile(const std::string &Filename,
294 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000295 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000296 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000297 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000298 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000299 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000300 return 0;
301 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000302}
Brian Gaeked0fde302003-11-11 22:41:34 +0000303