blob: 176715939257706d7331b2121cb3a0a0b8a1240c [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"
Reid Spencer32f55532006-06-07 23:18:34 +000022#include "llvm/System/Program.h"
Chris Lattner2d6481c2003-12-29 21:35:05 +000023#include <cerrno>
Reid Spencer0a834722004-12-21 07:51:33 +000024#include <iostream>
Duraid Madina0f7bfba2005-12-26 14:23:22 +000025#include <memory>
26
Chris Lattnerdeab9a72003-11-19 16:06:55 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnercb7e2e22003-10-18 05:54:18 +000029//===----------------------------------------------------------------------===//
30// BytecodeFileReader - Read from an mmap'able file descriptor.
31//
32
Misha Brukman12c29d12003-09-22 23:38:23 +000033namespace {
Misha Brukman12c29d12003-09-22 23:38:23 +000034 /// BytecodeFileReader - parses a bytecode file from a file
35 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000036 class BytecodeFileReader : public BytecodeReader {
Misha Brukman12c29d12003-09-22 23:38:23 +000037 private:
Reid Spencer9153f8f2004-12-13 18:25:27 +000038 sys::MappedFile mapFile;
Misha Brukman12c29d12003-09-22 23:38:23 +000039
40 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000041 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000042
43 public:
Reid Spencerdf45a542004-06-29 23:24:14 +000044 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
Misha Brukman12c29d12003-09-22 23:38:23 +000045 };
Misha Brukman12c29d12003-09-22 23:38:23 +000046}
47
Reid Spencerdf45a542004-06-29 23:24:14 +000048BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
Misha Brukman8a96c532005-04-21 21:44:41 +000049 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000050 : BytecodeReader(H)
Reid Spencerb152fd22006-08-22 16:10:12 +000051 , mapFile()
Reid Spencerdf45a542004-06-29 23:24:14 +000052{
Reid Spencerb152fd22006-08-22 16:10:12 +000053 std::string ErrMsg;
54 if (mapFile.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, &ErrMsg))
55 throw ErrMsg;
56 if (!mapFile.map(&ErrMsg))
57 throw ErrMsg;
Reid Spencer9153f8f2004-12-13 18:25:27 +000058 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
Reid Spencerb152fd22006-08-22 16:10:12 +000059 if (ParseBytecode(buffer, mapFile.size(), Filename, &ErrMsg)) {
60 throw ErrMsg;
61 }
Misha Brukman12c29d12003-09-22 23:38:23 +000062}
63
Chris Lattnercb7e2e22003-10-18 05:54:18 +000064//===----------------------------------------------------------------------===//
65// BytecodeBufferReader - Read from a memory buffer
66//
Misha Brukmand57308a2003-09-23 16:13:28 +000067
68namespace {
69 /// BytecodeBufferReader - parses a bytecode file from a buffer
70 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000071 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000072 private:
73 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000074 bool MustDelete;
75
76 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
77 void operator=(const BytecodeBufferReader &BFR); // Do not implement
78
79 public:
80 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000081 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000082 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000083 ~BytecodeBufferReader();
84
85 };
86}
87
88BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +000089 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000090 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000091 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000092 : BytecodeReader(H)
Misha Brukmand57308a2003-09-23 16:13:28 +000093{
94 // If not aligned, allocate a new buffer to hold the bytecode...
95 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +000096 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +000097 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +000098 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +000099 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000100 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000101 MustDelete = true;
102 } else {
103 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000104 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000105 MustDelete = false;
106 }
Reid Spencerb152fd22006-08-22 16:10:12 +0000107 std::string ErrMsg;
108 if (ParseBytecode(ParseBegin, Length, ModuleID, &ErrMsg)) {
Misha Brukman7f58de22003-10-08 19:55:47 +0000109 if (MustDelete) delete [] Buffer;
Reid Spencerb152fd22006-08-22 16:10:12 +0000110 throw ErrMsg;
Misha Brukman7f58de22003-10-08 19:55:47 +0000111 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000112}
113
114BytecodeBufferReader::~BytecodeBufferReader() {
115 if (MustDelete) delete [] Buffer;
116}
117
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000118//===----------------------------------------------------------------------===//
119// BytecodeStdinReader - Read bytecode from Standard Input
120//
Misha Brukmand57308a2003-09-23 16:13:28 +0000121
122namespace {
123 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000124 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000125 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000126 private:
127 std::vector<unsigned char> FileData;
128 unsigned char *FileBuf;
129
130 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
131 void operator=(const BytecodeStdinReader &BFR); // Do not implement
132
133 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000134 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Misha Brukmand57308a2003-09-23 16:13:28 +0000135 };
136}
Misha Brukman12c29d12003-09-22 23:38:23 +0000137
Misha Brukman8a96c532005-04-21 21:44:41 +0000138BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000139 : BytecodeReader(H)
140{
Reid Spencer32f55532006-06-07 23:18:34 +0000141 sys::Program::ChangeStdinToBinary();
Reid Spencer0a834722004-12-21 07:51:33 +0000142 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000143
144 // Read in all of the data from stdin, we cannot mmap stdin...
Reid Spencer0a834722004-12-21 07:51:33 +0000145 while (std::cin.good()) {
146 std::cin.read(Buffer, 4096*4);
147 int BlockSize = std::cin.gcount();
148 if (0 >= BlockSize)
149 break;
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];
Reid Spencerb152fd22006-08-22 16:10:12 +0000157 std::string ErrMsg;
158 if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", &ErrMsg)) {
159 throw ErrMsg;
160 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000161}
162
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000163//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000164// Varargs transmogrification code...
165//
166
167// CheckVarargs - This is used to automatically translate old-style varargs to
168// new style varargs for backwards compatibility.
169static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
170 Module* M = MP->getModule();
171
172 // check to see if va_start takes arguements...
173 Function* F = M->getNamedFunction("llvm.va_start");
174 if(F == 0) return MP; //No varargs use, just return.
175
176 if (F->getFunctionType()->getNumParams() == 1)
177 return MP; // Modern varargs processing, just return.
178
179 // If we get to this point, we know that we have an old-style module.
180 // Materialize the whole thing to perform the rewriting.
Chris Lattner0300f3e2006-07-06 21:35:01 +0000181 if (MP->materializeModule() == 0)
182 return 0;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000183
184 if(Function* F = M->getNamedFunction("llvm.va_start")) {
185 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000186
Andrew Lenharth558bc882005-06-18 18:34:52 +0000187 //foo = va_start()
188 // ->
189 //bar = alloca typeof(foo)
190 //va_start(bar)
191 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000192
Andrew Lenharth558bc882005-06-18 18:34:52 +0000193 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
194 const Type* ArgTy = F->getFunctionType()->getReturnType();
195 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000196 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000197 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000198
199 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
200 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
201 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
202 new CallInst(NF, bar, "", CI);
203 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
204 CI->replaceAllUsesWith(foo);
205 CI->getParent()->getInstList().erase(CI);
206 }
207 F->setName("");
208 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000209
Andrew Lenharth558bc882005-06-18 18:34:52 +0000210 if(Function* F = M->getNamedFunction("llvm.va_end")) {
211 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
212 //vaend foo
213 // ->
214 //bar = alloca 1 of typeof(foo)
215 //vaend bar
216 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
217 const Type* ArgTy = F->getFunctionType()->getParamType(0);
218 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000219 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000220 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000221
Andrew Lenharth558bc882005-06-18 18:34:52 +0000222 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
223 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
224 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000225 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000226 new CallInst(NF, bar, "", CI);
227 CI->getParent()->getInstList().erase(CI);
228 }
229 F->setName("");
230 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000231
Andrew Lenharth558bc882005-06-18 18:34:52 +0000232 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
233 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
234 //foo = vacopy(bar)
235 // ->
236 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000237 //b = alloca 1 of typeof(foo)
238 //store bar -> b
239 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000240 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000241
Andrew Lenharth558bc882005-06-18 18:34:52 +0000242 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
243 const Type* ArgTy = F->getFunctionType()->getReturnType();
244 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000245 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000246 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Andrew Lenharth558bc882005-06-18 18:34:52 +0000248 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
249 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
250 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000251 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
252 new StoreInst(CI->getOperand(1), b, CI);
253 new CallInst(NF, a, b, "", CI);
254 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000255 CI->replaceAllUsesWith(foo);
256 CI->getParent()->getInstList().erase(CI);
257 }
258 F->setName("");
259 }
260 return MP;
261}
262
263//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000264// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000265//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000266
267/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
268/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000269ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000270llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
271 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000272 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000273 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000274 return CheckVarargs(
275 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000276}
277
Misha Brukmand57308a2003-09-23 16:13:28 +0000278/// ParseBytecodeBuffer - Parse a given bytecode buffer
279///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000280Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
281 const std::string &ModuleID,
282 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000283 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000284 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000285 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
286 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000287 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000288 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000289 return 0;
290 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000291}
292
Misha Brukmand57308a2003-09-23 16:13:28 +0000293/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000294///
Reid Spencerdf45a542004-06-29 23:24:14 +0000295ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000296 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000297 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000298 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000299 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000300 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000301}
302
Misha Brukmand57308a2003-09-23 16:13:28 +0000303/// ParseBytecodeFile - Parse the given bytecode file
304///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000305Module *llvm::ParseBytecodeFile(const std::string &Filename,
306 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000307 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000308 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000309 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000310 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000311 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000312 return 0;
313 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000314}
Brian Gaeked0fde302003-11-11 22:41:34 +0000315
Reid Spencerdf45a542004-06-29 23:24:14 +0000316// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000317Module* llvm::AnalyzeBytecodeFile(
318 const std::string &Filename, ///< File to analyze
319 BytecodeAnalysis& bca, ///< Statistical output
320 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000321 std::ostream* output ///< Dump output
322)
Reid Spencerdf45a542004-06-29 23:24:14 +0000323{
324 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000325 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000326 std::auto_ptr<ModuleProvider> AMP(
327 getBytecodeModuleProvider(Filename,analyzerHandler));
328 return AMP->releaseModule();
329 } catch (std::string &err) {
330 if (ErrorStr) *ErrorStr = err;
331 return 0;
332 }
333}
334
335// AnalyzeBytecodeBuffer - analyze a buffer
336Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000337 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
338 unsigned Length, ///< Size of the bytecode buffer
339 const std::string& ModuleID, ///< Identifier for the module
340 BytecodeAnalysis& bca, ///< The results of the analysis
341 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000342 std::ostream* output ///< Dump output, if any
343)
Reid Spencerdf45a542004-06-29 23:24:14 +0000344{
345 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000346 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000347 std::auto_ptr<ModuleProvider>
348 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
349 return AMP->releaseModule();
350 } catch (std::string &err) {
351 if (ErrorStr) *ErrorStr = err;
352 return 0;
353 }
354}
355
Misha Brukman8a96c532005-04-21 21:44:41 +0000356bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000357 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000358 try {
359 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
360 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000361
Reid Spencere0cf59e2004-08-24 22:46:20 +0000362 deplibs = M->getLibraries();
363 delete M;
364 return true;
365 } catch (...) {
366 deplibs.clear();
367 return false;
368 }
369}
370
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000371static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000372 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000373 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000374 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000375 if (!GI->getName().empty())
376 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000377
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000378 // Loop over functions.
379 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
380 if (!FI->isExternal() && !FI->hasInternalLinkage())
381 if (!FI->getName().empty())
382 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000383}
384
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000385// Get just the externally visible defined symbols from the bytecode
386bool llvm::GetBytecodeSymbols(const sys::Path& fName,
387 std::vector<std::string>& symbols) {
Chris Lattner0300f3e2006-07-06 21:35:01 +0000388 std::auto_ptr<ModuleProvider> AMP(
389 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000390
Chris Lattner0300f3e2006-07-06 21:35:01 +0000391 // Get the module from the provider
392 Module* M = AMP->materializeModule();
393 if (M == 0) return false;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000394
Chris Lattner0300f3e2006-07-06 21:35:01 +0000395 // Get the symbols
396 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000397
Chris Lattner0300f3e2006-07-06 21:35:01 +0000398 // Done with the module
399 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000400}
401
Misha Brukman8a96c532005-04-21 21:44:41 +0000402ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000403llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000404 const std::string& ModuleID,
405 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000406
Reid Spencer5a885782004-11-16 06:41:05 +0000407 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000408 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000409 // Get the module provider
410 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000411
412 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000413 Module* M = MP->materializeModule();
Chris Lattner0300f3e2006-07-06 21:35:01 +0000414 if (M == 0) return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000415
416 // Get the symbols
417 getSymbols(M, symbols);
418
Reid Spencer5a885782004-11-16 06:41:05 +0000419 // Done with the module. Note that ModuleProvider will delete the
420 // Module when it is deleted. Also note that its the caller's responsibility
421 // to delete the ModuleProvider.
Reid Spencer766b7932004-11-15 01:20:11 +0000422 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000423
424 } catch (...) {
Reid Spencer93ee7dc2004-11-22 02:58:47 +0000425 // We delete only the ModuleProvider here because its destructor will
Reid Spencer5a885782004-11-16 06:41:05 +0000426 // also delete the Module (we used materializeModule not releaseModule).
427 delete MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000428 }
Reid Spencer766b7932004-11-15 01:20:11 +0000429 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000430}