blob: 1ee27185add7b5d22504e91640f016613ec1156f [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!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Andrew Lenharth558bc882005-06-18 18:34:52 +0000174 //foo = va_start()
175 // ->
176 //bar = alloca typeof(foo)
177 //va_start(bar)
178 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000179
Andrew Lenharth558bc882005-06-18 18:34:52 +0000180 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
181 const Type* ArgTy = F->getFunctionType()->getReturnType();
182 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000183 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000184 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000185
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 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000196
Andrew Lenharth558bc882005-06-18 18:34:52 +0000197 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);
Jeff Cohen00b168892005-07-27 06:12:32 +0000206 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000207 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000208
Andrew Lenharth558bc882005-06-18 18:34:52 +0000209 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);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000212 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000213 new CallInst(NF, bar, "", CI);
214 CI->getParent()->getInstList().erase(CI);
215 }
216 F->setName("");
217 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000218
Andrew Lenharth558bc882005-06-18 18:34:52 +0000219 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
220 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
221 //foo = vacopy(bar)
222 // ->
223 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000224 //b = alloca 1 of typeof(foo)
225 //store bar -> b
226 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000227 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Andrew Lenharth558bc882005-06-18 18:34:52 +0000229 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
230 const Type* ArgTy = F->getFunctionType()->getReturnType();
231 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000232 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000233 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000234
Andrew Lenharth558bc882005-06-18 18:34:52 +0000235 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
236 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
237 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000238 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
239 new StoreInst(CI->getOperand(1), b, CI);
240 new CallInst(NF, a, b, "", CI);
241 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000242 CI->replaceAllUsesWith(foo);
243 CI->getParent()->getInstList().erase(CI);
244 }
245 F->setName("");
246 }
247 return MP;
248}
249
250//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000251// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000252//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000253
254/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
255/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000256ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000257llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
258 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000259 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000260 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000261 return CheckVarargs(
262 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000263}
264
Misha Brukmand57308a2003-09-23 16:13:28 +0000265/// ParseBytecodeBuffer - Parse a given bytecode buffer
266///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000267Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
268 const std::string &ModuleID,
269 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000270 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000271 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000272 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
273 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000274 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000275 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000276 return 0;
277 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000278}
279
Misha Brukmand57308a2003-09-23 16:13:28 +0000280/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000281///
Reid Spencerdf45a542004-06-29 23:24:14 +0000282ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000283 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000284 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000285 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000286 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000287 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000288}
289
Misha Brukmand57308a2003-09-23 16:13:28 +0000290/// ParseBytecodeFile - Parse the given bytecode file
291///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000292Module *llvm::ParseBytecodeFile(const std::string &Filename,
293 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000294 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000295 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000296 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000297 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000298 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000299 return 0;
300 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000301}
Brian Gaeked0fde302003-11-11 22:41:34 +0000302
Reid Spencerdf45a542004-06-29 23:24:14 +0000303// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000304Module* llvm::AnalyzeBytecodeFile(
305 const std::string &Filename, ///< File to analyze
306 BytecodeAnalysis& bca, ///< Statistical output
307 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000308 std::ostream* output ///< Dump output
309)
Reid Spencerdf45a542004-06-29 23:24:14 +0000310{
311 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000312 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000313 std::auto_ptr<ModuleProvider> AMP(
314 getBytecodeModuleProvider(Filename,analyzerHandler));
315 return AMP->releaseModule();
316 } catch (std::string &err) {
317 if (ErrorStr) *ErrorStr = err;
318 return 0;
319 }
320}
321
322// AnalyzeBytecodeBuffer - analyze a buffer
323Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000324 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
325 unsigned Length, ///< Size of the bytecode buffer
326 const std::string& ModuleID, ///< Identifier for the module
327 BytecodeAnalysis& bca, ///< The results of the analysis
328 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000329 std::ostream* output ///< Dump output, if any
330)
Reid Spencerdf45a542004-06-29 23:24:14 +0000331{
332 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000333 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000334 std::auto_ptr<ModuleProvider>
335 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
336 return AMP->releaseModule();
337 } catch (std::string &err) {
338 if (ErrorStr) *ErrorStr = err;
339 return 0;
340 }
341}
342
Misha Brukman8a96c532005-04-21 21:44:41 +0000343bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000344 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000345 try {
346 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
347 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000348
Reid Spencere0cf59e2004-08-24 22:46:20 +0000349 deplibs = M->getLibraries();
350 delete M;
351 return true;
352 } catch (...) {
353 deplibs.clear();
354 return false;
355 }
356}
357
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000358static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000359 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000360 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000361 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000362 if (!GI->getName().empty())
363 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000364
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000365 // Loop over functions.
366 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
367 if (!FI->isExternal() && !FI->hasInternalLinkage())
368 if (!FI->getName().empty())
369 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000370}
371
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000372// Get just the externally visible defined symbols from the bytecode
373bool llvm::GetBytecodeSymbols(const sys::Path& fName,
374 std::vector<std::string>& symbols) {
375 try {
Misha Brukman8a96c532005-04-21 21:44:41 +0000376 std::auto_ptr<ModuleProvider> AMP(
Reid Spencer1fce0912004-12-11 00:14:15 +0000377 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000378
379 // Get the module from the provider
Reid Spencer5a885782004-11-16 06:41:05 +0000380 Module* M = AMP->materializeModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000381
Reid Spencer565ff3d2004-11-14 22:00:48 +0000382 // Get the symbols
383 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000384
385 // Done with the module
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000386 return true;
387
388 } catch (...) {
389 return false;
390 }
391}
392
Misha Brukman8a96c532005-04-21 21:44:41 +0000393ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000394llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000395 const std::string& ModuleID,
396 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000397
Reid Spencer5a885782004-11-16 06:41:05 +0000398 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000399 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000400 // Get the module provider
401 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000402
403 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000404 Module* M = MP->materializeModule();
Reid Spencer565ff3d2004-11-14 22:00:48 +0000405
406 // Get the symbols
407 getSymbols(M, symbols);
408
Reid Spencer5a885782004-11-16 06:41:05 +0000409 // Done with the module. Note that ModuleProvider will delete the
410 // Module when it is deleted. Also note that its the caller's responsibility
411 // to delete the ModuleProvider.
Reid Spencer766b7932004-11-15 01:20:11 +0000412 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000413
414 } catch (...) {
Reid Spencer93ee7dc2004-11-22 02:58:47 +0000415 // We delete only the ModuleProvider here because its destructor will
Reid Spencer5a885782004-11-16 06:41:05 +0000416 // also delete the Module (we used materializeModule not releaseModule).
417 delete MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000418 }
Reid Spencer766b7932004-11-15 01:20:11 +0000419 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000420}