blob: 1ea5c0262f19e0b230acf6ec466856a8a689101a [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>
Duraid Madina0f7bfba2005-12-26 14:23:22 +000024#include <memory>
25
Chris Lattnerdeab9a72003-11-19 16:06:55 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnercb7e2e22003-10-18 05:54:18 +000028//===----------------------------------------------------------------------===//
29// BytecodeFileReader - Read from an mmap'able file descriptor.
30//
31
Misha Brukman12c29d12003-09-22 23:38:23 +000032namespace {
Misha Brukman12c29d12003-09-22 23:38:23 +000033 /// BytecodeFileReader - parses a bytecode file from a file
34 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000035 class BytecodeFileReader : public BytecodeReader {
Misha Brukman12c29d12003-09-22 23:38:23 +000036 private:
Reid Spencer9153f8f2004-12-13 18:25:27 +000037 sys::MappedFile mapFile;
Misha Brukman12c29d12003-09-22 23:38:23 +000038
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:
Reid Spencerdf45a542004-06-29 23:24:14 +000043 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
Misha Brukman12c29d12003-09-22 23:38:23 +000044 };
Misha Brukman12c29d12003-09-22 23:38:23 +000045}
46
Reid Spencerdf45a542004-06-29 23:24:14 +000047BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
Misha Brukman8a96c532005-04-21 21:44:41 +000048 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000049 : BytecodeReader(H)
Reid Spencer9153f8f2004-12-13 18:25:27 +000050 , mapFile( sys::Path(Filename))
Reid Spencerdf45a542004-06-29 23:24:14 +000051{
Reid Spencer9153f8f2004-12-13 18:25:27 +000052 mapFile.map();
53 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
54 ParseBytecode(buffer, mapFile.size(), Filename);
Misha Brukman12c29d12003-09-22 23:38:23 +000055}
56
Chris Lattnercb7e2e22003-10-18 05:54:18 +000057//===----------------------------------------------------------------------===//
58// BytecodeBufferReader - Read from a memory buffer
59//
Misha Brukmand57308a2003-09-23 16:13:28 +000060
61namespace {
62 /// BytecodeBufferReader - parses a bytecode file from a buffer
63 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000064 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000065 private:
66 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000067 bool MustDelete;
68
69 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
70 void operator=(const BytecodeBufferReader &BFR); // Do not implement
71
72 public:
73 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000074 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000075 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000076 ~BytecodeBufferReader();
77
78 };
79}
80
81BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +000082 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000083 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000084 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000085 : BytecodeReader(H)
Misha Brukmand57308a2003-09-23 16:13:28 +000086{
87 // If not aligned, allocate a new buffer to hold the bytecode...
88 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +000089 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +000090 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +000091 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +000092 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +000093 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +000094 MustDelete = true;
95 } else {
96 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +000097 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +000098 MustDelete = false;
99 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000100 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000101 ParseBytecode(ParseBegin, Length, ModuleID);
Misha Brukman7f58de22003-10-08 19:55:47 +0000102 } catch (...) {
103 if (MustDelete) delete [] Buffer;
104 throw;
105 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000106}
107
108BytecodeBufferReader::~BytecodeBufferReader() {
109 if (MustDelete) delete [] Buffer;
110}
111
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000112//===----------------------------------------------------------------------===//
113// BytecodeStdinReader - Read bytecode from Standard Input
114//
Misha Brukmand57308a2003-09-23 16:13:28 +0000115
116namespace {
117 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000118 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000119 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000120 private:
121 std::vector<unsigned char> FileData;
122 unsigned char *FileBuf;
123
124 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
125 void operator=(const BytecodeStdinReader &BFR); // Do not implement
126
127 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000128 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Misha Brukmand57308a2003-09-23 16:13:28 +0000129 };
130}
Misha Brukman12c29d12003-09-22 23:38:23 +0000131
Misha Brukman8a96c532005-04-21 21:44:41 +0000132BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000133 : BytecodeReader(H)
134{
Reid Spencer0a834722004-12-21 07:51:33 +0000135 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000136
137 // Read in all of the data from stdin, we cannot mmap stdin...
Reid Spencer0a834722004-12-21 07:51:33 +0000138 while (std::cin.good()) {
139 std::cin.read(Buffer, 4096*4);
140 int BlockSize = std::cin.gcount();
141 if (0 >= BlockSize)
142 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000143 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
144 }
145
146 if (FileData.empty())
147 throw std::string("Standard Input empty!");
148
Misha Brukman12c29d12003-09-22 23:38:23 +0000149 FileBuf = &FileData[0];
Reid Spencer4542c432004-08-21 20:52:03 +0000150 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
Misha Brukman12c29d12003-09-22 23:38:23 +0000151}
152
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000153//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000154// Varargs transmogrification code...
155//
156
157// CheckVarargs - This is used to automatically translate old-style varargs to
158// new style varargs for backwards compatibility.
159static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
160 Module* M = MP->getModule();
161
162 // check to see if va_start takes arguements...
163 Function* F = M->getNamedFunction("llvm.va_start");
164 if(F == 0) return MP; //No varargs use, just return.
165
166 if (F->getFunctionType()->getNumParams() == 1)
167 return MP; // Modern varargs processing, just return.
168
169 // If we get to this point, we know that we have an old-style module.
170 // Materialize the whole thing to perform the rewriting.
171 MP->materializeModule();
172
173 if(Function* F = M->getNamedFunction("llvm.va_start")) {
174 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000175
Andrew Lenharth558bc882005-06-18 18:34:52 +0000176 //foo = va_start()
177 // ->
178 //bar = alloca typeof(foo)
179 //va_start(bar)
180 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000181
Andrew Lenharth558bc882005-06-18 18:34:52 +0000182 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
183 const Type* ArgTy = F->getFunctionType()->getReturnType();
184 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000185 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000186 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000187
188 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
189 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
190 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
191 new CallInst(NF, bar, "", CI);
192 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
193 CI->replaceAllUsesWith(foo);
194 CI->getParent()->getInstList().erase(CI);
195 }
196 F->setName("");
197 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000198
Andrew Lenharth558bc882005-06-18 18:34:52 +0000199 if(Function* F = M->getNamedFunction("llvm.va_end")) {
200 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
201 //vaend foo
202 // ->
203 //bar = alloca 1 of typeof(foo)
204 //vaend bar
205 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
206 const Type* ArgTy = F->getFunctionType()->getParamType(0);
207 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000208 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000209 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000210
Andrew Lenharth558bc882005-06-18 18:34:52 +0000211 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
212 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
213 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000214 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000215 new CallInst(NF, bar, "", CI);
216 CI->getParent()->getInstList().erase(CI);
217 }
218 F->setName("");
219 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000220
Andrew Lenharth558bc882005-06-18 18:34:52 +0000221 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
222 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
223 //foo = vacopy(bar)
224 // ->
225 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000226 //b = alloca 1 of typeof(foo)
227 //store bar -> b
228 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000229 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000230
Andrew Lenharth558bc882005-06-18 18:34:52 +0000231 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
232 const Type* ArgTy = F->getFunctionType()->getReturnType();
233 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000234 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000235 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000236
Andrew Lenharth558bc882005-06-18 18:34:52 +0000237 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
238 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
239 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000240 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
241 new StoreInst(CI->getOperand(1), b, CI);
242 new CallInst(NF, a, b, "", CI);
243 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000244 CI->replaceAllUsesWith(foo);
245 CI->getParent()->getInstList().erase(CI);
246 }
247 F->setName("");
248 }
249 return MP;
250}
251
252//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000253// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000254//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000255
256/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
257/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000258ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000259llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
260 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000261 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000262 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000263 return CheckVarargs(
264 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
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///
Reid Spencerdf45a542004-06-29 23:24:14 +0000284ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000285 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000286 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000287 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000288 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000289 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000290}
291
Misha Brukmand57308a2003-09-23 16:13:28 +0000292/// ParseBytecodeFile - Parse the given bytecode file
293///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000294Module *llvm::ParseBytecodeFile(const std::string &Filename,
295 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000296 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000297 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000298 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000299 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000300 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000301 return 0;
302 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000303}
Brian Gaeked0fde302003-11-11 22:41:34 +0000304
Reid Spencerdf45a542004-06-29 23:24:14 +0000305// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000306Module* llvm::AnalyzeBytecodeFile(
307 const std::string &Filename, ///< File to analyze
308 BytecodeAnalysis& bca, ///< Statistical output
309 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000310 std::ostream* output ///< Dump output
311)
Reid Spencerdf45a542004-06-29 23:24:14 +0000312{
313 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000314 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000315 std::auto_ptr<ModuleProvider> AMP(
316 getBytecodeModuleProvider(Filename,analyzerHandler));
317 return AMP->releaseModule();
318 } catch (std::string &err) {
319 if (ErrorStr) *ErrorStr = err;
320 return 0;
321 }
322}
323
324// AnalyzeBytecodeBuffer - analyze a buffer
325Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000326 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
327 unsigned Length, ///< Size of the bytecode buffer
328 const std::string& ModuleID, ///< Identifier for the module
329 BytecodeAnalysis& bca, ///< The results of the analysis
330 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000331 std::ostream* output ///< Dump output, if any
332)
Reid Spencerdf45a542004-06-29 23:24:14 +0000333{
334 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000335 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000336 std::auto_ptr<ModuleProvider>
337 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
338 return AMP->releaseModule();
339 } catch (std::string &err) {
340 if (ErrorStr) *ErrorStr = err;
341 return 0;
342 }
343}
344
Misha Brukman8a96c532005-04-21 21:44:41 +0000345bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000346 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000347 try {
348 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
349 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000350
Reid Spencere0cf59e2004-08-24 22:46:20 +0000351 deplibs = M->getLibraries();
352 delete M;
353 return true;
354 } catch (...) {
355 deplibs.clear();
356 return false;
357 }
358}
359
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000360static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000361 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000362 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000363 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000364 if (!GI->getName().empty())
365 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000366
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000367 // Loop over functions.
368 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
369 if (!FI->isExternal() && !FI->hasInternalLinkage())
370 if (!FI->getName().empty())
371 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000372}
373
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000374// Get just the externally visible defined symbols from the bytecode
375bool llvm::GetBytecodeSymbols(const sys::Path& fName,
376 std::vector<std::string>& symbols) {
377 try {
Misha Brukman8a96c532005-04-21 21:44:41 +0000378 std::auto_ptr<ModuleProvider> AMP(
Reid Spencer1fce0912004-12-11 00:14:15 +0000379 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000380
381 // Get the module from the provider
Reid Spencer5a885782004-11-16 06:41:05 +0000382 Module* M = AMP->materializeModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000383
Reid Spencer565ff3d2004-11-14 22:00:48 +0000384 // Get the symbols
385 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000386
387 // Done with the module
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000388 return true;
389
390 } catch (...) {
391 return false;
392 }
393}
394
Misha Brukman8a96c532005-04-21 21:44:41 +0000395ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000396llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000397 const std::string& ModuleID,
398 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000399
Reid Spencer5a885782004-11-16 06:41:05 +0000400 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000401 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000402 // Get the module provider
403 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000404
405 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000406 Module* M = MP->materializeModule();
Reid Spencer565ff3d2004-11-14 22:00:48 +0000407
408 // Get the symbols
409 getSymbols(M, symbols);
410
Reid Spencer5a885782004-11-16 06:41:05 +0000411 // Done with the module. Note that ModuleProvider will delete the
412 // Module when it is deleted. Also note that its the caller's responsibility
413 // to delete the ModuleProvider.
Reid Spencer766b7932004-11-15 01:20:11 +0000414 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000415
416 } catch (...) {
Reid Spencer93ee7dc2004-11-22 02:58:47 +0000417 // We delete only the ModuleProvider here because its destructor will
Reid Spencer5a885782004-11-16 06:41:05 +0000418 // also delete the Module (we used materializeModule not releaseModule).
419 delete MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000420 }
Reid Spencer766b7932004-11-15 01:20:11 +0000421 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000422}