blob: be1541707ef1e6002217c84bb7b530b6341e0ee7 [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"
Misha Brukman12c29d12003-09-22 23:38:23 +000021#include "Config/unistd.h"
Chris Lattner2d6481c2003-12-29 21:35:05 +000022#include <cerrno>
Chris Lattnerdeab9a72003-11-19 16:06:55 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnercb7e2e22003-10-18 05:54:18 +000025//===----------------------------------------------------------------------===//
26// BytecodeFileReader - Read from an mmap'able file descriptor.
27//
28
Misha Brukman12c29d12003-09-22 23:38:23 +000029namespace {
Misha Brukman12c29d12003-09-22 23:38:23 +000030 /// BytecodeFileReader - parses a bytecode file from a file
31 ///
32 class BytecodeFileReader : public BytecodeParser {
33 private:
34 unsigned char *Buffer;
Chris Lattnerfb777c22004-05-28 00:24:41 +000035 unsigned Length;
Misha Brukman12c29d12003-09-22 23:38:23 +000036
37 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000038 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000039
40 public:
41 BytecodeFileReader(const std::string &Filename);
42 ~BytecodeFileReader();
Misha Brukman12c29d12003-09-22 23:38:23 +000043 };
Misha Brukman12c29d12003-09-22 23:38:23 +000044}
45
Brian Gaeke27b40bc2003-12-12 00:47:44 +000046static std::string ErrnoMessage (int savedErrNum, std::string descr) {
47 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
48}
49
Misha Brukman12c29d12003-09-22 23:38:23 +000050BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
Chris Lattnerfb777c22004-05-28 00:24:41 +000051 Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
52 if (Buffer == 0)
53 throw "Error reading file '" + Filename + "'.";
Misha Brukman12c29d12003-09-22 23:38:23 +000054
Misha Brukman7f58de22003-10-08 19:55:47 +000055 try {
56 // Parse the bytecode we mmapped in
57 ParseBytecode(Buffer, Length, Filename);
58 } catch (...) {
Chris Lattnerfb777c22004-05-28 00:24:41 +000059 UnmapFileFromAddressSpace(Buffer, Length);
Misha Brukman7f58de22003-10-08 19:55:47 +000060 throw;
61 }
Misha Brukman12c29d12003-09-22 23:38:23 +000062}
63
64BytecodeFileReader::~BytecodeFileReader() {
65 // Unmmap the bytecode...
Chris Lattnerfb777c22004-05-28 00:24:41 +000066 UnmapFileFromAddressSpace(Buffer, Length);
Misha Brukman12c29d12003-09-22 23:38:23 +000067}
68
Chris Lattnercb7e2e22003-10-18 05:54:18 +000069//===----------------------------------------------------------------------===//
70// BytecodeBufferReader - Read from a memory buffer
71//
Misha Brukmand57308a2003-09-23 16:13:28 +000072
73namespace {
74 /// BytecodeBufferReader - parses a bytecode file from a buffer
75 ///
76 class BytecodeBufferReader : public BytecodeParser {
77 private:
78 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000079 bool MustDelete;
80
81 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
82 void operator=(const BytecodeBufferReader &BFR); // Do not implement
83
84 public:
85 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
86 const std::string &ModuleID);
87 ~BytecodeBufferReader();
88
89 };
90}
91
92BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +000093 unsigned Length,
Misha Brukmand57308a2003-09-23 16:13:28 +000094 const std::string &ModuleID)
95{
96 // If not aligned, allocate a new buffer to hold the bytecode...
97 const unsigned char *ParseBegin = 0;
Misha Brukmand57308a2003-09-23 16:13:28 +000098 if ((intptr_t)Buf & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +000099 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000100 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000101 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000102 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000103 MustDelete = true;
104 } else {
105 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000106 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000107 MustDelete = false;
108 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000109 try {
110 ParseBytecode(ParseBegin, Length, ModuleID);
111 } catch (...) {
112 if (MustDelete) delete [] Buffer;
113 throw;
114 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000115}
116
117BytecodeBufferReader::~BytecodeBufferReader() {
118 if (MustDelete) delete [] Buffer;
119}
120
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000121//===----------------------------------------------------------------------===//
122// BytecodeStdinReader - Read bytecode from Standard Input
123//
Misha Brukmand57308a2003-09-23 16:13:28 +0000124
125namespace {
126 /// BytecodeStdinReader - parses a bytecode file from stdin
127 ///
128 class BytecodeStdinReader : public BytecodeParser {
129 private:
130 std::vector<unsigned char> FileData;
131 unsigned char *FileBuf;
132
133 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
134 void operator=(const BytecodeStdinReader &BFR); // Do not implement
135
136 public:
137 BytecodeStdinReader();
Misha Brukmand57308a2003-09-23 16:13:28 +0000138 };
139}
Misha Brukman12c29d12003-09-22 23:38:23 +0000140
Misha Brukman12c29d12003-09-22 23:38:23 +0000141BytecodeStdinReader::BytecodeStdinReader() {
142 int BlockSize;
143 unsigned char Buffer[4096*4];
144
145 // Read in all of the data from stdin, we cannot mmap stdin...
Brian Gaeked0fde302003-11-11 22:41:34 +0000146 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000147 if (BlockSize == -1)
Brian Gaeke27b40bc2003-12-12 00:47:44 +0000148 throw ErrnoMessage(errno, "read from standard input");
Misha Brukmand57308a2003-09-23 16:13:28 +0000149
Misha Brukman12c29d12003-09-22 23:38:23 +0000150 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
151 }
152
153 if (FileData.empty())
154 throw std::string("Standard Input empty!");
155
Misha Brukman12c29d12003-09-22 23:38:23 +0000156 FileBuf = &FileData[0];
Misha Brukman12c29d12003-09-22 23:38:23 +0000157 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
158}
159
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000160//===----------------------------------------------------------------------===//
161// Varargs transmogrification code...
Misha Brukmand57308a2003-09-23 16:13:28 +0000162//
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000163
164// CheckVarargs - This is used to automatically translate old-style varargs to
165// new style varargs for backwards compatibility.
166static ModuleProvider *CheckVarargs(ModuleProvider *MP) {
167 Module *M = MP->getModule();
168
169 // Check to see if va_start takes arguments...
170 Function *F = M->getNamedFunction("llvm.va_start");
171 if (F == 0) return MP; // No varargs use, just return.
172
173 if (F->getFunctionType()->getNumParams() == 0)
174 return MP; // Modern varargs processing, just return.
175
176 // If we get to this point, we know that we have an old-style module.
177 // Materialize the whole thing to perform the rewriting.
178 MP->materializeModule();
179
180 // If the user is making use of obsolete varargs intrinsics, adjust them for
181 // the user.
182 if (Function *F = M->getNamedFunction("llvm.va_start")) {
183 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
184
185 const Type *RetTy = F->getFunctionType()->getParamType(0);
186 RetTy = cast<PointerType>(RetTy)->getElementType();
187 Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
188
189 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
190 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
191 Value *V = new CallInst(NF, "", CI);
192 new StoreInst(V, CI->getOperand(1), CI);
193 CI->getParent()->getInstList().erase(CI);
194 }
195 F->setName("");
196 }
197
198 if (Function *F = M->getNamedFunction("llvm.va_end")) {
199 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
200 const Type *ArgTy = F->getFunctionType()->getParamType(0);
201 ArgTy = cast<PointerType>(ArgTy)->getElementType();
202 Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
203 ArgTy, 0);
204
205 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
206 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
207 Value *V = new LoadInst(CI->getOperand(1), "", CI);
208 new CallInst(NF, V, "", CI);
209 CI->getParent()->getInstList().erase(CI);
210 }
211 F->setName("");
212 }
213
214 if (Function *F = M->getNamedFunction("llvm.va_copy")) {
215 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
216 const Type *ArgTy = F->getFunctionType()->getParamType(0);
217 ArgTy = cast<PointerType>(ArgTy)->getElementType();
218 Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
219 ArgTy, 0);
220
221 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
222 if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
223 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
224 new StoreInst(V, CI->getOperand(1), CI);
225 CI->getParent()->getInstList().erase(CI);
226 }
227 F->setName("");
228 }
229 return MP;
230}
231
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000232//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000233// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000234//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000235
236/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
237/// buffer
Chris Lattner00413e32003-10-04 20:14:59 +0000238ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000239llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
240 unsigned Length,
241 const std::string &ModuleID) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000242 return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
Misha Brukman12c29d12003-09-22 23:38:23 +0000243}
244
Misha Brukmand57308a2003-09-23 16:13:28 +0000245/// ParseBytecodeBuffer - Parse a given bytecode buffer
246///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000247Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
248 const std::string &ModuleID,
249 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000250 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000251 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000252 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
253 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000254 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000255 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000256 return 0;
257 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000258}
259
Misha Brukmand57308a2003-09-23 16:13:28 +0000260/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000261///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000262ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000263 if (Filename != std::string("-")) // Read from a file...
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000264 return CheckVarargs(new BytecodeFileReader(Filename));
Misha Brukman12c29d12003-09-22 23:38:23 +0000265 else // Read from stdin
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000266 return CheckVarargs(new BytecodeStdinReader());
Misha Brukman12c29d12003-09-22 23:38:23 +0000267}
268
Misha Brukmand57308a2003-09-23 16:13:28 +0000269/// ParseBytecodeFile - Parse the given bytecode file
270///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000271Module *llvm::ParseBytecodeFile(const std::string &Filename,
272 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000273 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000274 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000275 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}
Brian Gaeked0fde302003-11-11 22:41:34 +0000281