blob: 8d1f3849861a6e08ae50e2a8146dffe13cf8f6d5 [file] [log] [blame]
Misha Brukman46453792003-09-22 23:44:46 +00001//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
Misha Brukman8a96c532005-04-21 21:44:41 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman8a96c532005-04-21 21:44:41 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Reid Spencerdf45a542004-06-29 23:24:14 +000015#include "llvm/Bytecode/Analyzer.h"
Chris Lattnerdeab9a72003-11-19 16:06:55 +000016#include "llvm/Bytecode/Reader.h"
Reid Spencerdf45a542004-06-29 23:24:14 +000017#include "Reader.h"
Chris Lattnercb7e2e22003-10-18 05:54:18 +000018#include "llvm/Module.h"
19#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/StringExtras.h"
Reid Spencer9153f8f2004-12-13 18:25:27 +000021#include "llvm/System/MappedFile.h"
Chris Lattner2d6481c2003-12-29 21:35:05 +000022#include <cerrno>
Reid Spencer0a834722004-12-21 07:51:33 +000023#include <iostream>
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 Brukman12c29d12003-09-22 23:38:23 +000031 /// BytecodeFileReader - parses a bytecode file from a file
32 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000033 class BytecodeFileReader : public BytecodeReader {
Misha Brukman12c29d12003-09-22 23:38:23 +000034 private:
Reid Spencer9153f8f2004-12-13 18:25:27 +000035 sys::MappedFile mapFile;
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:
Reid Spencerdf45a542004-06-29 23:24:14 +000041 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
Misha Brukman12c29d12003-09-22 23:38:23 +000042 };
Misha Brukman12c29d12003-09-22 23:38:23 +000043}
44
Reid Spencerdf45a542004-06-29 23:24:14 +000045BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
Misha Brukman8a96c532005-04-21 21:44:41 +000046 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000047 : BytecodeReader(H)
Reid Spencer9153f8f2004-12-13 18:25:27 +000048 , mapFile( sys::Path(Filename))
Reid Spencerdf45a542004-06-29 23:24:14 +000049{
Reid Spencer9153f8f2004-12-13 18:25:27 +000050 mapFile.map();
51 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
52 ParseBytecode(buffer, mapFile.size(), Filename);
Misha Brukman12c29d12003-09-22 23:38:23 +000053}
54
Chris Lattnercb7e2e22003-10-18 05:54:18 +000055//===----------------------------------------------------------------------===//
56// BytecodeBufferReader - Read from a memory buffer
57//
Misha Brukmand57308a2003-09-23 16:13:28 +000058
59namespace {
60 /// BytecodeBufferReader - parses a bytecode file from a buffer
61 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000062 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000063 private:
64 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000065 bool MustDelete;
66
67 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
68 void operator=(const BytecodeBufferReader &BFR); // Do not implement
69
70 public:
71 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000072 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000073 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000074 ~BytecodeBufferReader();
75
76 };
77}
78
79BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +000080 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000081 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000082 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000083 : BytecodeReader(H)
Misha Brukmand57308a2003-09-23 16:13:28 +000084{
85 // If not aligned, allocate a new buffer to hold the bytecode...
86 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +000087 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +000088 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +000089 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +000090 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +000091 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +000092 MustDelete = true;
93 } else {
94 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +000095 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +000096 MustDelete = false;
97 }
Misha Brukman7f58de22003-10-08 19:55:47 +000098 try {
Reid Spencer4542c432004-08-21 20:52:03 +000099 ParseBytecode(ParseBegin, Length, ModuleID);
Misha Brukman7f58de22003-10-08 19:55:47 +0000100 } catch (...) {
101 if (MustDelete) delete [] Buffer;
102 throw;
103 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000104}
105
106BytecodeBufferReader::~BytecodeBufferReader() {
107 if (MustDelete) delete [] Buffer;
108}
109
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000110//===----------------------------------------------------------------------===//
111// BytecodeStdinReader - Read bytecode from Standard Input
112//
Misha Brukmand57308a2003-09-23 16:13:28 +0000113
114namespace {
115 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000116 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000117 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000118 private:
119 std::vector<unsigned char> FileData;
120 unsigned char *FileBuf;
121
122 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
123 void operator=(const BytecodeStdinReader &BFR); // Do not implement
124
125 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000126 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Misha Brukmand57308a2003-09-23 16:13:28 +0000127 };
128}
Misha Brukman12c29d12003-09-22 23:38:23 +0000129
Misha Brukman8a96c532005-04-21 21:44:41 +0000130BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000131 : BytecodeReader(H)
132{
Reid Spencer0a834722004-12-21 07:51:33 +0000133 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000134
135 // Read in all of the data from stdin, we cannot mmap stdin...
Reid Spencer0a834722004-12-21 07:51:33 +0000136 while (std::cin.good()) {
137 std::cin.read(Buffer, 4096*4);
138 int BlockSize = std::cin.gcount();
139 if (0 >= BlockSize)
140 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000141 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
142 }
143
144 if (FileData.empty())
145 throw std::string("Standard Input empty!");
146
Misha Brukman12c29d12003-09-22 23:38:23 +0000147 FileBuf = &FileData[0];
Reid Spencer4542c432004-08-21 20:52:03 +0000148 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
Misha Brukman12c29d12003-09-22 23:38:23 +0000149}
150
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000151//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000152// Varargs transmogrification code...
153//
154
155// CheckVarargs - This is used to automatically translate old-style varargs to
156// new style varargs for backwards compatibility.
157static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
158 Module* M = MP->getModule();
159
160 // check to see if va_start takes arguements...
161 Function* F = M->getNamedFunction("llvm.va_start");
162 if(F == 0) return MP; //No varargs use, just return.
163
164 if (F->getFunctionType()->getNumParams() == 1)
165 return MP; // Modern varargs processing, just return.
166
167 // If we get to this point, we know that we have an old-style module.
168 // Materialize the whole thing to perform the rewriting.
169 MP->materializeModule();
170
171 if(Function* F = M->getNamedFunction("llvm.va_start")) {
172 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
173
174 //foo = va_start()
175 // ->
176 //bar = alloca typeof(foo)
177 //va_start(bar)
178 //foo = load bar
179
180 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
181 const Type* ArgTy = F->getFunctionType()->getReturnType();
182 const Type* ArgTyPtr = PointerType::get(ArgTy);
183 Function* NF = M->getOrInsertFunction("llvm.va_start",
184 RetTy, ArgTyPtr, 0);
185
186 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
187 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
188 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
189 new CallInst(NF, bar, "", CI);
190 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
191 CI->replaceAllUsesWith(foo);
192 CI->getParent()->getInstList().erase(CI);
193 }
194 F->setName("");
195 }
196
197 if(Function* F = M->getNamedFunction("llvm.va_end")) {
198 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
199 //vaend foo
200 // ->
201 //bar = alloca 1 of typeof(foo)
202 //vaend bar
203 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
204 const Type* ArgTy = F->getFunctionType()->getParamType(0);
205 const Type* ArgTyPtr = PointerType::get(ArgTy);
206 Function* NF = M->getOrInsertFunction("llvm.va_end",
207 RetTy, ArgTyPtr, 0);
208
209 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
210 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
211 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
212 new CallInst(NF, bar, "", CI);
213 CI->getParent()->getInstList().erase(CI);
214 }
215 F->setName("");
216 }
217
218 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
219 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
220 //foo = vacopy(bar)
221 // ->
222 //a = alloca 1 of typeof(foo)
223 //vacopy(a, bar)
224 //foo = load a
225
226 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
227 const Type* ArgTy = F->getFunctionType()->getReturnType();
228 const Type* ArgTyPtr = PointerType::get(ArgTy);
229 Function* NF = M->getOrInsertFunction("llvm.va_copy",
230 RetTy, ArgTyPtr, 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 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
235 new CallInst(NF, a, CI->getOperand(1), "", CI);
236 Value* foo = new LoadInst(a, "vacopy.fix.2", CI);
237 CI->replaceAllUsesWith(foo);
238 CI->getParent()->getInstList().erase(CI);
239 }
240 F->setName("");
241 }
242 return MP;
243}
244
245//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000246// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000247//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000248
249/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
250/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000251ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000252llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
253 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000254 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000255 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000256 return CheckVarargs(
257 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
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///
Reid Spencerdf45a542004-06-29 23:24:14 +0000277ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000278 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000279 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000280 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000281 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000282 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000283}
284
Misha Brukmand57308a2003-09-23 16:13:28 +0000285/// ParseBytecodeFile - Parse the given bytecode file
286///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000287Module *llvm::ParseBytecodeFile(const std::string &Filename,
288 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000289 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000290 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000291 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000292 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000293 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000294 return 0;
295 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000296}
Brian Gaeked0fde302003-11-11 22:41:34 +0000297
Reid Spencerdf45a542004-06-29 23:24:14 +0000298// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000299Module* llvm::AnalyzeBytecodeFile(
300 const std::string &Filename, ///< File to analyze
301 BytecodeAnalysis& bca, ///< Statistical output
302 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000303 std::ostream* output ///< Dump output
304)
Reid Spencerdf45a542004-06-29 23:24:14 +0000305{
306 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000307 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000308 std::auto_ptr<ModuleProvider> AMP(
309 getBytecodeModuleProvider(Filename,analyzerHandler));
310 return AMP->releaseModule();
311 } catch (std::string &err) {
312 if (ErrorStr) *ErrorStr = err;
313 return 0;
314 }
315}
316
317// AnalyzeBytecodeBuffer - analyze a buffer
318Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000319 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
320 unsigned Length, ///< Size of the bytecode buffer
321 const std::string& ModuleID, ///< Identifier for the module
322 BytecodeAnalysis& bca, ///< The results of the analysis
323 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000324 std::ostream* output ///< Dump output, if any
325)
Reid Spencerdf45a542004-06-29 23:24:14 +0000326{
327 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000328 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000329 std::auto_ptr<ModuleProvider>
330 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
331 return AMP->releaseModule();
332 } catch (std::string &err) {
333 if (ErrorStr) *ErrorStr = err;
334 return 0;
335 }
336}
337
Misha Brukman8a96c532005-04-21 21:44:41 +0000338bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000339 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000340 try {
341 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
342 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000343
Reid Spencere0cf59e2004-08-24 22:46:20 +0000344 deplibs = M->getLibraries();
345 delete M;
346 return true;
347 } catch (...) {
348 deplibs.clear();
349 return false;
350 }
351}
352
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000353static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000354 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000355 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000356 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000357 if (!GI->getName().empty())
358 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000359
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000360 // Loop over functions.
361 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
362 if (!FI->isExternal() && !FI->hasInternalLinkage())
363 if (!FI->getName().empty())
364 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000365}
366
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000367// Get just the externally visible defined symbols from the bytecode
368bool llvm::GetBytecodeSymbols(const sys::Path& fName,
369 std::vector<std::string>& symbols) {
370 try {
Misha Brukman8a96c532005-04-21 21:44:41 +0000371 std::auto_ptr<ModuleProvider> AMP(
Reid Spencer1fce0912004-12-11 00:14:15 +0000372 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000373
374 // Get the module from the provider
Reid Spencer5a885782004-11-16 06:41:05 +0000375 Module* M = AMP->materializeModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000376
Reid Spencer565ff3d2004-11-14 22:00:48 +0000377 // Get the symbols
378 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000379
380 // Done with the module
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000381 return true;
382
383 } catch (...) {
384 return false;
385 }
386}
387
Misha Brukman8a96c532005-04-21 21:44:41 +0000388ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000389llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000390 const std::string& ModuleID,
391 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000392
Reid Spencer5a885782004-11-16 06:41:05 +0000393 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000394 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000395 // Get the module provider
396 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000397
398 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000399 Module* M = MP->materializeModule();
Reid Spencer565ff3d2004-11-14 22:00:48 +0000400
401 // Get the symbols
402 getSymbols(M, symbols);
403
Reid Spencer5a885782004-11-16 06:41:05 +0000404 // Done with the module. Note that ModuleProvider will delete the
405 // Module when it is deleted. Also note that its the caller's responsibility
406 // to delete the ModuleProvider.
Reid Spencer766b7932004-11-15 01:20:11 +0000407 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000408
409 } catch (...) {
Reid Spencer93ee7dc2004-11-22 02:58:47 +0000410 // We delete only the ModuleProvider here because its destructor will
Reid Spencer5a885782004-11-16 06:41:05 +0000411 // also delete the Module (we used materializeModule not releaseModule).
412 delete MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000413 }
Reid Spencer766b7932004-11-15 01:20:11 +0000414 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000415}