blob: d45a0f79bf38ada8e26030ab91a5881267121490 [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>
Duraid Madina0f7bfba2005-12-26 14:23:22 +000024#include <memory>
Chris Lattnerdeab9a72003-11-19 16:06:55 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnercb7e2e22003-10-18 05:54:18 +000027//===----------------------------------------------------------------------===//
28// BytecodeFileReader - Read from an mmap'able file descriptor.
29//
30
Misha Brukman12c29d12003-09-22 23:38:23 +000031namespace {
Misha Brukman12c29d12003-09-22 23:38:23 +000032 /// BytecodeFileReader - parses a bytecode file from a file
33 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000034 class BytecodeFileReader : public BytecodeReader {
Misha Brukman12c29d12003-09-22 23:38:23 +000035 private:
Reid Spencer0b5a5042006-08-25 17:43:11 +000036 std::string fileName;
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);
Reid Spencer0b5a5042006-08-25 17:43:11 +000044 bool read(std::string* ErrMsg);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000045
46 void freeState() {
47 BytecodeReader::freeState();
48 mapFile.close();
49 }
Misha Brukman12c29d12003-09-22 23:38:23 +000050 };
Misha Brukman12c29d12003-09-22 23:38:23 +000051}
52
Reid Spencerdf45a542004-06-29 23:24:14 +000053BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000054 llvm::BytecodeHandler* H)
55 : BytecodeReader(H), fileName(Filename) {
Reid Spencer0b5a5042006-08-25 17:43:11 +000056}
57
58bool BytecodeFileReader::read(std::string* ErrMsg) {
59 if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
60 return true;
61 if (!mapFile.map(ErrMsg)) {
62 mapFile.close();
63 return true;
Reid Spencerb152fd22006-08-22 16:10:12 +000064 }
Reid Spencer0b5a5042006-08-25 17:43:11 +000065 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000066 return ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg);
Misha Brukman12c29d12003-09-22 23:38:23 +000067}
68
Chris Lattnercb7e2e22003-10-18 05:54:18 +000069//===----------------------------------------------------------------------===//
70// BytecodeBufferReader - Read from a memory buffer
71//
Misha Brukmand57308a2003-09-23 16:13:28 +000072
73namespace {
74 /// BytecodeBufferReader - parses a bytecode file from a buffer
75 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000076 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000077 private:
78 const unsigned char *Buffer;
Reid Spencer0b5a5042006-08-25 17:43:11 +000079 const unsigned char *Buf;
80 unsigned Length;
81 std::string ModuleID;
Misha Brukmand57308a2003-09-23 16:13:28 +000082 bool MustDelete;
83
84 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
85 void operator=(const BytecodeBufferReader &BFR); // Do not implement
86
87 public:
88 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000089 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000090 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000091 ~BytecodeBufferReader();
92
Reid Spencer0b5a5042006-08-25 17:43:11 +000093 bool read(std::string* ErrMsg);
94
Misha Brukmand57308a2003-09-23 16:13:28 +000095 };
96}
97
Reid Spencer0b5a5042006-08-25 17:43:11 +000098BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
99 unsigned len,
100 const std::string &modID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000101 llvm::BytecodeHandler *H)
102 : BytecodeReader(H), Buffer(0), Buf(buf), Length(len), ModuleID(modID)
103 , MustDelete(false) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000104}
105
106BytecodeBufferReader::~BytecodeBufferReader() {
107 if (MustDelete) delete [] Buffer;
108}
109
110bool
111BytecodeBufferReader::read(std::string* ErrMsg) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000112 // If not aligned, allocate a new buffer to hold the bytecode...
113 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +0000114 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000115 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000116 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000117 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000118 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000119 MustDelete = true;
120 } else {
121 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000122 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000123 MustDelete = false;
124 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000125 if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
Misha Brukman7f58de22003-10-08 19:55:47 +0000126 if (MustDelete) delete [] Buffer;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000127 return true;
Misha Brukman7f58de22003-10-08 19:55:47 +0000128 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000129 return false;
Misha Brukmand57308a2003-09-23 16:13:28 +0000130}
131
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000132//===----------------------------------------------------------------------===//
133// BytecodeStdinReader - Read bytecode from Standard Input
134//
Misha Brukmand57308a2003-09-23 16:13:28 +0000135
136namespace {
137 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000138 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000139 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000140 private:
141 std::vector<unsigned char> FileData;
142 unsigned char *FileBuf;
143
144 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
145 void operator=(const BytecodeStdinReader &BFR); // Do not implement
146
147 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000148 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Reid Spencer0b5a5042006-08-25 17:43:11 +0000149 bool read(std::string* ErrMsg);
Misha Brukmand57308a2003-09-23 16:13:28 +0000150 };
151}
Misha Brukman12c29d12003-09-22 23:38:23 +0000152
Misha Brukman8a96c532005-04-21 21:44:41 +0000153BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000154 : BytecodeReader(H)
155{
Reid Spencer0b5a5042006-08-25 17:43:11 +0000156}
157
158bool
159BytecodeStdinReader::read(std::string* ErrMsg)
160{
Reid Spencer32f55532006-06-07 23:18:34 +0000161 sys::Program::ChangeStdinToBinary();
Reid Spencer0a834722004-12-21 07:51:33 +0000162 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000163
164 // Read in all of the data from stdin, we cannot mmap stdin...
Bill Wendlingbcd24982006-12-07 20:28:15 +0000165 while (cin.stream()->good()) {
166 cin.stream()->read(Buffer, 4096*4);
167 int BlockSize = cin.stream()->gcount();
Reid Spencer0a834722004-12-21 07:51:33 +0000168 if (0 >= BlockSize)
169 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000170 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
171 }
172
Reid Spencer0b5a5042006-08-25 17:43:11 +0000173 if (FileData.empty()) {
174 if (ErrMsg)
175 *ErrMsg = "Standard Input is empty!";
176 return true;
177 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000178
Misha Brukman12c29d12003-09-22 23:38:23 +0000179 FileBuf = &FileData[0];
Reid Spencer0b5a5042006-08-25 17:43:11 +0000180 if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
181 return true;
182 return false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000183}
184
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000185//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000186// Varargs transmogrification code...
187//
188
189// CheckVarargs - This is used to automatically translate old-style varargs to
190// new style varargs for backwards compatibility.
191static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
192 Module* M = MP->getModule();
193
194 // check to see if va_start takes arguements...
195 Function* F = M->getNamedFunction("llvm.va_start");
196 if(F == 0) return MP; //No varargs use, just return.
197
198 if (F->getFunctionType()->getNumParams() == 1)
199 return MP; // Modern varargs processing, just return.
200
201 // If we get to this point, we know that we have an old-style module.
202 // Materialize the whole thing to perform the rewriting.
Chris Lattner0300f3e2006-07-06 21:35:01 +0000203 if (MP->materializeModule() == 0)
204 return 0;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000205
206 if(Function* F = M->getNamedFunction("llvm.va_start")) {
207 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000208
Andrew Lenharth558bc882005-06-18 18:34:52 +0000209 //foo = va_start()
210 // ->
211 //bar = alloca typeof(foo)
212 //va_start(bar)
213 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Andrew Lenharth558bc882005-06-18 18:34:52 +0000215 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
216 const Type* ArgTy = F->getFunctionType()->getReturnType();
217 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000218 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000219 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000220
221 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
222 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
223 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
224 new CallInst(NF, bar, "", CI);
225 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
226 CI->replaceAllUsesWith(foo);
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_end")) {
233 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
234 //vaend foo
235 // ->
236 //bar = alloca 1 of typeof(foo)
237 //vaend bar
238 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
239 const Type* ArgTy = F->getFunctionType()->getParamType(0);
240 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000241 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000242 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Andrew Lenharth558bc882005-06-18 18:34:52 +0000244 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
245 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
246 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000247 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000248 new CallInst(NF, bar, "", CI);
249 CI->getParent()->getInstList().erase(CI);
250 }
251 F->setName("");
252 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000253
Andrew Lenharth558bc882005-06-18 18:34:52 +0000254 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
255 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
256 //foo = vacopy(bar)
257 // ->
258 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000259 //b = alloca 1 of typeof(foo)
260 //store bar -> b
261 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000262 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000263
Andrew Lenharth558bc882005-06-18 18:34:52 +0000264 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
265 const Type* ArgTy = F->getFunctionType()->getReturnType();
266 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000267 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000268 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000269
Andrew Lenharth558bc882005-06-18 18:34:52 +0000270 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
271 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
272 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000273 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
274 new StoreInst(CI->getOperand(1), b, CI);
275 new CallInst(NF, a, b, "", CI);
276 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000277 CI->replaceAllUsesWith(foo);
278 CI->getParent()->getInstList().erase(CI);
279 }
280 F->setName("");
281 }
282 return MP;
283}
284
285//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000286// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000287//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000288
289/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
290/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000291ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000292llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
293 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000294 const std::string &ModuleID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000295 std::string *ErrMsg,
296 BytecodeHandler *H) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000297 BytecodeBufferReader* rdr =
298 new BytecodeBufferReader(Buffer, Length, ModuleID, H);
299 if (rdr->read(ErrMsg))
300 return 0;
301 return CheckVarargs(rdr);
Misha Brukman12c29d12003-09-22 23:38:23 +0000302}
303
Misha Brukmand57308a2003-09-23 16:13:28 +0000304/// ParseBytecodeBuffer - Parse a given bytecode buffer
305///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000306Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
307 const std::string &ModuleID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000308 std::string *ErrMsg) {
309 ModuleProvider *MP =
Reid Spencer0b5a5042006-08-25 17:43:11 +0000310 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000311 if (!MP) return 0;
312 Module *M = MP->releaseModule();
313 delete MP;
314 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +0000315}
316
Misha Brukmand57308a2003-09-23 16:13:28 +0000317/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000318///
Reid Spencer0b5a5042006-08-25 17:43:11 +0000319ModuleProvider *
320llvm::getBytecodeModuleProvider(const std::string &Filename,
321 std::string* ErrMsg,
322 BytecodeHandler* H) {
323 // Read from a file
324 if (Filename != std::string("-")) {
325 BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
326 if (rdr->read(ErrMsg))
327 return 0;
328 return CheckVarargs(rdr);
329 }
330
331 // Read from stdin
332 BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
333 if (rdr->read(ErrMsg))
334 return 0;
335 return CheckVarargs(rdr);
Misha Brukman12c29d12003-09-22 23:38:23 +0000336}
337
Misha Brukmand57308a2003-09-23 16:13:28 +0000338/// ParseBytecodeFile - Parse the given bytecode file
339///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000340Module *llvm::ParseBytecodeFile(const std::string &Filename,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000341 std::string *ErrMsg) {
342 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000343 if (!MP) return 0;
344 Module *M = MP->releaseModule();
345 delete MP;
346 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +0000347}
Brian Gaeked0fde302003-11-11 22:41:34 +0000348
Reid Spencerdf45a542004-06-29 23:24:14 +0000349// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000350Module* llvm::AnalyzeBytecodeFile(
351 const std::string &Filename, ///< File to analyze
352 BytecodeAnalysis& bca, ///< Statistical output
Reid Spencer0b5a5042006-08-25 17:43:11 +0000353 std::string *ErrMsg, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000354 std::ostream* output ///< Dump output
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000355) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000356 BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
357 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000358 if (!MP) return 0;
359 Module *M = MP->releaseModule();
360 delete MP;
361 return M;
Reid Spencerdf45a542004-06-29 23:24:14 +0000362}
363
364// AnalyzeBytecodeBuffer - analyze a buffer
365Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000366 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
367 unsigned Length, ///< Size of the bytecode buffer
368 const std::string& ModuleID, ///< Identifier for the module
369 BytecodeAnalysis& bca, ///< The results of the analysis
Reid Spencer0b5a5042006-08-25 17:43:11 +0000370 std::string* ErrMsg, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000371 std::ostream* output ///< Dump output, if any
372)
Reid Spencerdf45a542004-06-29 23:24:14 +0000373{
Reid Spencer0b5a5042006-08-25 17:43:11 +0000374 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
375 ModuleProvider* MP =
376 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000377 if (!MP) return 0;
378 Module *M = MP->releaseModule();
379 delete MP;
380 return M;
Reid Spencerdf45a542004-06-29 23:24:14 +0000381}
382
Misha Brukman8a96c532005-04-21 21:44:41 +0000383bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000384 Module::LibraryListType& deplibs,
385 std::string* ErrMsg) {
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000386 ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
Reid Spencer0b5a5042006-08-25 17:43:11 +0000387 if (!MP) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000388 deplibs.clear();
Reid Spencer0b5a5042006-08-25 17:43:11 +0000389 return true;
Reid Spencere0cf59e2004-08-24 22:46:20 +0000390 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000391 Module* M = MP->releaseModule();
392 deplibs = M->getLibraries();
393 delete M;
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000394 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000395 return false;
Reid Spencere0cf59e2004-08-24 22:46:20 +0000396}
397
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000398static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000399 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000400 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000401 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000402 if (!GI->getName().empty())
403 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000404
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000405 // Loop over functions.
406 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
407 if (!FI->isExternal() && !FI->hasInternalLinkage())
408 if (!FI->getName().empty())
409 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000410}
411
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000412// Get just the externally visible defined symbols from the bytecode
413bool llvm::GetBytecodeSymbols(const sys::Path& fName,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000414 std::vector<std::string>& symbols,
415 std::string* ErrMsg) {
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000416 ModuleProvider *MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
Reid Spencer0b5a5042006-08-25 17:43:11 +0000417 if (!MP)
418 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000419
Chris Lattner0300f3e2006-07-06 21:35:01 +0000420 // Get the module from the provider
Reid Spencer0b5a5042006-08-25 17:43:11 +0000421 Module* M = MP->materializeModule();
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000422 if (M == 0) {
423 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000424 return true;
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000425 }
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000426
Chris Lattner0300f3e2006-07-06 21:35:01 +0000427 // Get the symbols
428 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000429
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000430 // Done with the module.
431 delete MP;
Chris Lattner0300f3e2006-07-06 21:35:01 +0000432 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000433}
434
Misha Brukman8a96c532005-04-21 21:44:41 +0000435ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000436llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000437 const std::string& ModuleID,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000438 std::vector<std::string>& symbols,
439 std::string* ErrMsg) {
440 // Get the module provider
441 ModuleProvider* MP =
442 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
443 if (!MP)
444 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000445
Reid Spencer0b5a5042006-08-25 17:43:11 +0000446 // Get the module from the provider
447 Module* M = MP->materializeModule();
448 if (M == 0) {
Reid Spencer5a885782004-11-16 06:41:05 +0000449 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000450 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000451 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000452
453 // Get the symbols
454 getSymbols(M, symbols);
455
456 // Done with the module. Note that ModuleProvider will delete the
457 // Module when it is deleted. Also note that its the caller's responsibility
458 // to delete the ModuleProvider.
459 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000460}