blob: b9e7cd29aacf488b502c9a50f43da4ad863c0630 [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.
Chris Lattner0300f3e2006-07-06 21:35:01 +0000173 if (MP->materializeModule() == 0)
174 return 0;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000175
176 if(Function* F = M->getNamedFunction("llvm.va_start")) {
177 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000178
Andrew Lenharth558bc882005-06-18 18:34:52 +0000179 //foo = va_start()
180 // ->
181 //bar = alloca typeof(foo)
182 //va_start(bar)
183 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000184
Andrew Lenharth558bc882005-06-18 18:34:52 +0000185 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
186 const Type* ArgTy = F->getFunctionType()->getReturnType();
187 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000188 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000189 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000190
191 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
192 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
193 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
194 new CallInst(NF, bar, "", CI);
195 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
196 CI->replaceAllUsesWith(foo);
197 CI->getParent()->getInstList().erase(CI);
198 }
199 F->setName("");
200 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Andrew Lenharth558bc882005-06-18 18:34:52 +0000202 if(Function* F = M->getNamedFunction("llvm.va_end")) {
203 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
204 //vaend foo
205 // ->
206 //bar = alloca 1 of typeof(foo)
207 //vaend bar
208 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
209 const Type* ArgTy = F->getFunctionType()->getParamType(0);
210 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000211 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000212 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000213
Andrew Lenharth558bc882005-06-18 18:34:52 +0000214 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
215 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
216 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000217 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000218 new CallInst(NF, bar, "", CI);
219 CI->getParent()->getInstList().erase(CI);
220 }
221 F->setName("");
222 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000223
Andrew Lenharth558bc882005-06-18 18:34:52 +0000224 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
225 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
226 //foo = vacopy(bar)
227 // ->
228 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000229 //b = alloca 1 of typeof(foo)
230 //store bar -> b
231 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000232 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Andrew Lenharth558bc882005-06-18 18:34:52 +0000234 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
235 const Type* ArgTy = F->getFunctionType()->getReturnType();
236 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000237 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000238 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000239
Andrew Lenharth558bc882005-06-18 18:34:52 +0000240 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
241 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
242 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000243 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
244 new StoreInst(CI->getOperand(1), b, CI);
245 new CallInst(NF, a, b, "", CI);
246 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000247 CI->replaceAllUsesWith(foo);
248 CI->getParent()->getInstList().erase(CI);
249 }
250 F->setName("");
251 }
252 return MP;
253}
254
255//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000256// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000257//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000258
259/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
260/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000261ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000262llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
263 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000264 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +0000265 BytecodeHandler* H ) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000266 return CheckVarargs(
267 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000268}
269
Misha Brukmand57308a2003-09-23 16:13:28 +0000270/// ParseBytecodeBuffer - Parse a given bytecode buffer
271///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000272Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
273 const std::string &ModuleID,
274 std::string *ErrorStr){
Misha Brukmand57308a2003-09-23 16:13:28 +0000275 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000276 std::auto_ptr<ModuleProvider>
Chris Lattnera9833592003-10-04 19:19:37 +0000277 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
278 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000279 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000280 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000281 return 0;
282 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000283}
284
Misha Brukmand57308a2003-09-23 16:13:28 +0000285/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000286///
Reid Spencerdf45a542004-06-29 23:24:14 +0000287ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
Reid Spencer97c7d742004-07-04 11:03:03 +0000288 BytecodeHandler* H) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000289 if (Filename != std::string("-")) // Read from a file...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000290 return CheckVarargs(new BytecodeFileReader(Filename,H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000291 else // Read from stdin
Andrew Lenharth558bc882005-06-18 18:34:52 +0000292 return CheckVarargs(new BytecodeStdinReader(H));
Misha Brukman12c29d12003-09-22 23:38:23 +0000293}
294
Misha Brukmand57308a2003-09-23 16:13:28 +0000295/// ParseBytecodeFile - Parse the given bytecode file
296///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000297Module *llvm::ParseBytecodeFile(const std::string &Filename,
298 std::string *ErrorStr) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000299 try {
Chris Lattner00413e32003-10-04 20:14:59 +0000300 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
Chris Lattnera9833592003-10-04 19:19:37 +0000301 return AMP->releaseModule();
Misha Brukmand57308a2003-09-23 16:13:28 +0000302 } catch (std::string &err) {
Misha Brukman134aba62003-09-24 22:10:47 +0000303 if (ErrorStr) *ErrorStr = err;
Misha Brukmand57308a2003-09-23 16:13:28 +0000304 return 0;
305 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000306}
Brian Gaeked0fde302003-11-11 22:41:34 +0000307
Reid Spencerdf45a542004-06-29 23:24:14 +0000308// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000309Module* llvm::AnalyzeBytecodeFile(
310 const std::string &Filename, ///< File to analyze
311 BytecodeAnalysis& bca, ///< Statistical output
312 std::string *ErrorStr, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000313 std::ostream* output ///< Dump output
314)
Reid Spencerdf45a542004-06-29 23:24:14 +0000315{
316 try {
Misha Brukman7df00742004-09-12 20:47:33 +0000317 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000318 std::auto_ptr<ModuleProvider> AMP(
319 getBytecodeModuleProvider(Filename,analyzerHandler));
320 return AMP->releaseModule();
321 } catch (std::string &err) {
322 if (ErrorStr) *ErrorStr = err;
323 return 0;
324 }
325}
326
327// AnalyzeBytecodeBuffer - analyze a buffer
328Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000329 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
330 unsigned Length, ///< Size of the bytecode buffer
331 const std::string& ModuleID, ///< Identifier for the module
332 BytecodeAnalysis& bca, ///< The results of the analysis
333 std::string* ErrorStr, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000334 std::ostream* output ///< Dump output, if any
335)
Reid Spencerdf45a542004-06-29 23:24:14 +0000336{
337 try {
Reid Spencer4542c432004-08-21 20:52:03 +0000338 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
Reid Spencerdf45a542004-06-29 23:24:14 +0000339 std::auto_ptr<ModuleProvider>
340 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
341 return AMP->releaseModule();
342 } catch (std::string &err) {
343 if (ErrorStr) *ErrorStr = err;
344 return 0;
345 }
346}
347
Misha Brukman8a96c532005-04-21 21:44:41 +0000348bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Misha Brukman7df00742004-09-12 20:47:33 +0000349 Module::LibraryListType& deplibs) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000350 try {
351 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
352 Module* M = AMP->releaseModule();
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000353
Reid Spencere0cf59e2004-08-24 22:46:20 +0000354 deplibs = M->getLibraries();
355 delete M;
356 return true;
357 } catch (...) {
358 deplibs.clear();
359 return false;
360 }
361}
362
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000363static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000364 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000365 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000366 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000367 if (!GI->getName().empty())
368 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000369
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000370 // Loop over functions.
371 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
372 if (!FI->isExternal() && !FI->hasInternalLinkage())
373 if (!FI->getName().empty())
374 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000375}
376
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000377// Get just the externally visible defined symbols from the bytecode
378bool llvm::GetBytecodeSymbols(const sys::Path& fName,
379 std::vector<std::string>& symbols) {
Chris Lattner0300f3e2006-07-06 21:35:01 +0000380 std::auto_ptr<ModuleProvider> AMP(
381 getBytecodeModuleProvider(fName.toString()));
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000382
Chris Lattner0300f3e2006-07-06 21:35:01 +0000383 // Get the module from the provider
384 Module* M = AMP->materializeModule();
385 if (M == 0) return false;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000386
Chris Lattner0300f3e2006-07-06 21:35:01 +0000387 // Get the symbols
388 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000389
Chris Lattner0300f3e2006-07-06 21:35:01 +0000390 // Done with the module
391 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000392}
393
Misha Brukman8a96c532005-04-21 21:44:41 +0000394ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000395llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000396 const std::string& ModuleID,
397 std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000398
Reid Spencer5a885782004-11-16 06:41:05 +0000399 ModuleProvider* MP = 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000400 try {
Reid Spencer5a885782004-11-16 06:41:05 +0000401 // Get the module provider
402 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
Reid Spencer565ff3d2004-11-14 22:00:48 +0000403
404 // Get the module from the provider
Reid Spencer766b7932004-11-15 01:20:11 +0000405 Module* M = MP->materializeModule();
Chris Lattner0300f3e2006-07-06 21:35:01 +0000406 if (M == 0) return 0;
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}