blob: 956d1ede696fa1ce1bb4ac1e0e1138d035fced62 [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 Spencer9153f8f2004-12-13 18:25:27 +000051 , mapFile( sys::Path(Filename))
Reid Spencerdf45a542004-06-29 23:24:14 +000052{
Reid Spencer9153f8f2004-12-13 18:25:27 +000053 mapFile.map();
54 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
55 ParseBytecode(buffer, mapFile.size(), Filename);
Misha Brukman12c29d12003-09-22 23:38:23 +000056}
57
Chris Lattnercb7e2e22003-10-18 05:54:18 +000058//===----------------------------------------------------------------------===//
59// BytecodeBufferReader - Read from a memory buffer
60//
Misha Brukmand57308a2003-09-23 16:13:28 +000061
62namespace {
63 /// BytecodeBufferReader - parses a bytecode file from a buffer
64 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000065 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000066 private:
67 const unsigned char *Buffer;
Misha Brukmand57308a2003-09-23 16:13:28 +000068 bool MustDelete;
69
70 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
71 void operator=(const BytecodeBufferReader &BFR); // Do not implement
72
73 public:
74 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000075 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000076 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000077 ~BytecodeBufferReader();
78
79 };
80}
81
82BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
Misha Brukman34ce14b2003-09-24 22:04:02 +000083 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000084 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000085 llvm::BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +000086 : BytecodeReader(H)
Misha Brukmand57308a2003-09-23 16:13:28 +000087{
88 // If not aligned, allocate a new buffer to hold the bytecode...
89 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +000090 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +000091 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +000092 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +000093 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +000094 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +000095 MustDelete = true;
96 } else {
97 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +000098 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +000099 MustDelete = false;
100 }
Misha Brukman7f58de22003-10-08 19:55:47 +0000101 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000102 ParseBytecode(ParseBegin, Length, ModuleID);
Misha Brukman7f58de22003-10-08 19:55:47 +0000103 } catch (...) {
104 if (MustDelete) delete [] Buffer;
105 throw;
106 }
Misha Brukmand57308a2003-09-23 16:13:28 +0000107}
108
109BytecodeBufferReader::~BytecodeBufferReader() {
110 if (MustDelete) delete [] Buffer;
111}
112
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000113//===----------------------------------------------------------------------===//
114// BytecodeStdinReader - Read bytecode from Standard Input
115//
Misha Brukmand57308a2003-09-23 16:13:28 +0000116
117namespace {
118 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000119 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000120 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000121 private:
122 std::vector<unsigned char> FileData;
123 unsigned char *FileBuf;
124
125 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
126 void operator=(const BytecodeStdinReader &BFR); // Do not implement
127
128 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000129 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Misha Brukmand57308a2003-09-23 16:13:28 +0000130 };
131}
Misha Brukman12c29d12003-09-22 23:38:23 +0000132
Misha Brukman8a96c532005-04-21 21:44:41 +0000133BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000134 : BytecodeReader(H)
135{
Reid Spencer32f55532006-06-07 23:18:34 +0000136 sys::Program::ChangeStdinToBinary();
Reid Spencer0a834722004-12-21 07:51:33 +0000137 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000138
139 // Read in all of the data from stdin, we cannot mmap stdin...
Reid Spencer0a834722004-12-21 07:51:33 +0000140 while (std::cin.good()) {
141 std::cin.read(Buffer, 4096*4);
142 int BlockSize = std::cin.gcount();
143 if (0 >= BlockSize)
144 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000145 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
146 }
147
148 if (FileData.empty())
149 throw std::string("Standard Input empty!");
150
Misha Brukman12c29d12003-09-22 23:38:23 +0000151 FileBuf = &FileData[0];
Reid Spencer4542c432004-08-21 20:52:03 +0000152 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
Misha Brukman12c29d12003-09-22 23:38:23 +0000153}
154
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000155//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000156// Varargs transmogrification code...
157//
158
159// CheckVarargs - This is used to automatically translate old-style varargs to
160// new style varargs for backwards compatibility.
161static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
162 Module* M = MP->getModule();
163
164 // check to see if va_start takes arguements...
165 Function* F = M->getNamedFunction("llvm.va_start");
166 if(F == 0) return MP; //No varargs use, just return.
167
168 if (F->getFunctionType()->getNumParams() == 1)
169 return MP; // Modern varargs processing, just return.
170
171 // If we get to this point, we know that we have an old-style module.
172 // Materialize the whole thing to perform the rewriting.
173 MP->materializeModule();
174
175 if(Function* F = M->getNamedFunction("llvm.va_start")) {
176 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000177
Andrew Lenharth558bc882005-06-18 18:34:52 +0000178 //foo = va_start()
179 // ->
180 //bar = alloca typeof(foo)
181 //va_start(bar)
182 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000183
Andrew Lenharth558bc882005-06-18 18:34:52 +0000184 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
185 const Type* ArgTy = F->getFunctionType()->getReturnType();
186 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000187 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000188 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000189
190 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
191 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
192 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
193 new CallInst(NF, bar, "", CI);
194 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
195 CI->replaceAllUsesWith(foo);
196 CI->getParent()->getInstList().erase(CI);
197 }
198 F->setName("");
199 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000200
Andrew Lenharth558bc882005-06-18 18:34:52 +0000201 if(Function* F = M->getNamedFunction("llvm.va_end")) {
202 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
203 //vaend foo
204 // ->
205 //bar = alloca 1 of typeof(foo)
206 //vaend bar
207 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
208 const Type* ArgTy = F->getFunctionType()->getParamType(0);
209 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000210 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000211 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000212
Andrew Lenharth558bc882005-06-18 18:34:52 +0000213 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
214 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
215 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000216 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000217 new CallInst(NF, bar, "", CI);
218 CI->getParent()->getInstList().erase(CI);
219 }
220 F->setName("");
221 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000222
Andrew Lenharth558bc882005-06-18 18:34:52 +0000223 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
224 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
225 //foo = vacopy(bar)
226 // ->
227 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000228 //b = alloca 1 of typeof(foo)
229 //store bar -> b
230 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000231 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000232
Andrew Lenharth558bc882005-06-18 18:34:52 +0000233 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
234 const Type* ArgTy = F->getFunctionType()->getReturnType();
235 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000236 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000237 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000238
Andrew Lenharth558bc882005-06-18 18:34:52 +0000239 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
240 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
241 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000242 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
243 new StoreInst(CI->getOperand(1), b, CI);
244 new CallInst(NF, a, b, "", CI);
245 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000246 CI->replaceAllUsesWith(foo);
247 CI->getParent()->getInstList().erase(CI);
248 }
249 F->setName("");
250 }
251 return MP;
252}
253
254//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000255// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000256//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000257
258/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
259/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000260ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000261llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
262 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000263 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000264 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000265 return CheckVarargs(
266 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000267}
268
Misha Brukmand57308a2003-09-23 16:13:28 +0000269/// ParseBytecodeBuffer - Parse a given bytecode buffer
270///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000271Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
272 const std::string &ModuleID,
273 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000274 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000275 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000276 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
277 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000278 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000279 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000280 return 0;
281 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000282}
283
Misha Brukmand57308a2003-09-23 16:13:28 +0000284/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000285///
Reid Spencerdf45a542004-06-29 23:24:14 +0000286ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000287 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000288 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000289 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000290 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000291 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000292}
293
Misha Brukmand57308a2003-09-23 16:13:28 +0000294/// ParseBytecodeFile - Parse the given bytecode file
295///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000296Module *llvm::ParseBytecodeFile(const std::string &Filename,
297 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000298 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000299 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000300 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000301 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000302 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000303 return 0;
304 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000305}
Brian Gaeked0fde302003-11-11 22:41:34 +0000306
Reid Spencerdf45a542004-06-29 23:24:14 +0000307// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000308Module* llvm::AnalyzeBytecodeFile(
309 const std::string &Filename, ///< File to analyze
310 BytecodeAnalysis& bca, ///< Statistical output
311 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000312 std::ostream* output ///< Dump output
313)
Reid Spencerdf45a542004-06-29 23:24:14 +0000314{
315 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000316 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000317 std::auto_ptr<ModuleProvider> AMP(
318 getBytecodeModuleProvider(Filename,analyzerHandler));
319 return AMP->releaseModule();
320 } catch (std::string &err) {
321 if (ErrorStr) *ErrorStr = err;
322 return 0;
323 }
324}
325
326// AnalyzeBytecodeBuffer - analyze a buffer
327Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000328 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
329 unsigned Length, ///< Size of the bytecode buffer
330 const std::string& ModuleID, ///< Identifier for the module
331 BytecodeAnalysis& bca, ///< The results of the analysis
332 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000333 std::ostream* output ///< Dump output, if any
334)
Reid Spencerdf45a542004-06-29 23:24:14 +0000335{
336 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000337 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000338 std::auto_ptr<ModuleProvider>
339 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
340 return AMP->releaseModule();
341 } catch (std::string &err) {
342 if (ErrorStr) *ErrorStr = err;
343 return 0;
344 }
345}
346
Misha Brukman8a96c532005-04-21 21:44:41 +0000347bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000348 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000349 try {
350 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
351 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000352
Reid Spencere0cf59e2004-08-24 22:46:20 +0000353 deplibs = M->getLibraries();
354 delete M;
355 return true;
356 } catch (...) {
357 deplibs.clear();
358 return false;
359 }
360}
361
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000362static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000363 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000364 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000365 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000366 if (!GI->getName().empty())
367 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000368
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000369 // Loop over functions.
370 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
371 if (!FI->isExternal() && !FI->hasInternalLinkage())
372 if (!FI->getName().empty())
373 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000374}
375
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000376// Get just the externally visible defined symbols from the bytecode
377bool llvm::GetBytecodeSymbols(const sys::Path& fName,
378 std::vector<std::string>& symbols) {
379 try {
Misha Brukman8a96c532005-04-21 21:44:41 +0000380 std::auto_ptr<ModuleProvider> AMP(
Reid Spencer1fce0912004-12-11 00:14:15 +0000381 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000382
383 // Get the module from the provider
Reid Spencer5a885782004-11-16 06:41:05 +0000384 Module* M = AMP->materializeModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000385
Reid Spencer565ff3d2004-11-14 22:00:48 +0000386 // Get the symbols
387 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000388
389 // Done with the module
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000390 return true;
391
392 } catch (...) {
393 return false;
394 }
395}
396
Misha Brukman8a96c532005-04-21 21:44:41 +0000397ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000398llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000399 const std::string& ModuleID,
400 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000401
Reid Spencer5a885782004-11-16 06:41:05 +0000402 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000403 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000404 // Get the module provider
405 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000406
407 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000408 Module* M = MP->materializeModule();
Reid Spencer565ff3d2004-11-14 22:00:48 +0000409
410 // Get the symbols
411 getSymbols(M, symbols);
412
Reid Spencer5a885782004-11-16 06:41:05 +0000413 // Done with the module. Note that ModuleProvider will delete the
414 // Module when it is deleted. Also note that its the caller's responsibility
415 // to delete the ModuleProvider.
Reid Spencer766b7932004-11-15 01:20:11 +0000416 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000417
418 } catch (...) {
Reid Spencer93ee7dc2004-11-22 02:58:47 +0000419 // We delete only the ModuleProvider here because its destructor will
Reid Spencer5a885782004-11-16 06:41:05 +0000420 // also delete the Module (we used materializeModule not releaseModule).
421 delete MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000422 }
Reid Spencer766b7932004-11-15 01:20:11 +0000423 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000424}