blob: c12067266992ad6d664fb898c033d4fd4205c8b3 [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 Spencer0b5a5042006-08-25 17:43:11 +000038 std::string fileName;
Reid Spencer9153f8f2004-12-13 18:25:27 +000039 sys::MappedFile mapFile;
Misha Brukman12c29d12003-09-22 23:38:23 +000040
41 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
Misha Brukman5c344412003-09-23 15:09:26 +000042 void operator=(const BytecodeFileReader &BFR); // Do not implement
Misha Brukman12c29d12003-09-22 23:38:23 +000043
44 public:
Reid Spencerdf45a542004-06-29 23:24:14 +000045 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
Reid Spencer0b5a5042006-08-25 17:43:11 +000046 bool read(std::string* ErrMsg);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000047
48 void freeState() {
49 BytecodeReader::freeState();
50 mapFile.close();
51 }
Misha Brukman12c29d12003-09-22 23:38:23 +000052 };
Misha Brukman12c29d12003-09-22 23:38:23 +000053}
54
Reid Spencerdf45a542004-06-29 23:24:14 +000055BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000056 llvm::BytecodeHandler* H)
57 : BytecodeReader(H), fileName(Filename) {
Reid Spencer0b5a5042006-08-25 17:43:11 +000058}
59
60bool BytecodeFileReader::read(std::string* ErrMsg) {
61 if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
62 return true;
63 if (!mapFile.map(ErrMsg)) {
64 mapFile.close();
65 return true;
Reid Spencerb152fd22006-08-22 16:10:12 +000066 }
Reid Spencer0b5a5042006-08-25 17:43:11 +000067 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
Chris Lattnerf0edc6c2006-10-12 18:32:30 +000068 return ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg);
Misha Brukman12c29d12003-09-22 23:38:23 +000069}
70
Chris Lattnercb7e2e22003-10-18 05:54:18 +000071//===----------------------------------------------------------------------===//
72// BytecodeBufferReader - Read from a memory buffer
73//
Misha Brukmand57308a2003-09-23 16:13:28 +000074
75namespace {
76 /// BytecodeBufferReader - parses a bytecode file from a buffer
77 ///
Reid Spencerdf45a542004-06-29 23:24:14 +000078 class BytecodeBufferReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +000079 private:
80 const unsigned char *Buffer;
Reid Spencer0b5a5042006-08-25 17:43:11 +000081 const unsigned char *Buf;
82 unsigned Length;
83 std::string ModuleID;
Misha Brukmand57308a2003-09-23 16:13:28 +000084 bool MustDelete;
85
86 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
87 void operator=(const BytecodeBufferReader &BFR); // Do not implement
88
89 public:
90 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +000091 const std::string &ModuleID,
Reid Spencer97c7d742004-07-04 11:03:03 +000092 llvm::BytecodeHandler* Handler = 0);
Misha Brukmand57308a2003-09-23 16:13:28 +000093 ~BytecodeBufferReader();
94
Reid Spencer0b5a5042006-08-25 17:43:11 +000095 bool read(std::string* ErrMsg);
96
Misha Brukmand57308a2003-09-23 16:13:28 +000097 };
98}
99
Reid Spencer0b5a5042006-08-25 17:43:11 +0000100BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
101 unsigned len,
102 const std::string &modID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000103 llvm::BytecodeHandler *H)
104 : BytecodeReader(H), Buffer(0), Buf(buf), Length(len), ModuleID(modID)
105 , MustDelete(false) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000106}
107
108BytecodeBufferReader::~BytecodeBufferReader() {
109 if (MustDelete) delete [] Buffer;
110}
111
112bool
113BytecodeBufferReader::read(std::string* ErrMsg) {
Misha Brukmand57308a2003-09-23 16:13:28 +0000114 // If not aligned, allocate a new buffer to hold the bytecode...
115 const unsigned char *ParseBegin = 0;
Reid Spencer9a7e0c52004-08-04 22:56:46 +0000116 if (reinterpret_cast<uint64_t>(Buf) & 3) {
Misha Brukman34ce14b2003-09-24 22:04:02 +0000117 Buffer = new unsigned char[Length+4];
Chris Lattner4eed7932003-09-24 22:34:17 +0000118 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
Misha Brukmand57308a2003-09-23 16:13:28 +0000119 ParseBegin = Buffer + Offset;
Misha Brukman34ce14b2003-09-24 22:04:02 +0000120 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
Misha Brukmand57308a2003-09-23 16:13:28 +0000121 MustDelete = true;
122 } else {
123 // If we don't need to copy it over, just use the caller's copy
John Criswell4dcbd5e2003-09-23 21:19:11 +0000124 ParseBegin = Buffer = Buf;
Misha Brukmand57308a2003-09-23 16:13:28 +0000125 MustDelete = false;
126 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000127 if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
Misha Brukman7f58de22003-10-08 19:55:47 +0000128 if (MustDelete) delete [] Buffer;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000129 return true;
Misha Brukman7f58de22003-10-08 19:55:47 +0000130 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000131 return false;
Misha Brukmand57308a2003-09-23 16:13:28 +0000132}
133
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000134//===----------------------------------------------------------------------===//
135// BytecodeStdinReader - Read bytecode from Standard Input
136//
Misha Brukmand57308a2003-09-23 16:13:28 +0000137
138namespace {
139 /// BytecodeStdinReader - parses a bytecode file from stdin
Misha Brukman8a96c532005-04-21 21:44:41 +0000140 ///
Reid Spencerdf45a542004-06-29 23:24:14 +0000141 class BytecodeStdinReader : public BytecodeReader {
Misha Brukmand57308a2003-09-23 16:13:28 +0000142 private:
143 std::vector<unsigned char> FileData;
144 unsigned char *FileBuf;
145
146 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
147 void operator=(const BytecodeStdinReader &BFR); // Do not implement
148
149 public:
Reid Spencerdf45a542004-06-29 23:24:14 +0000150 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
Reid Spencer0b5a5042006-08-25 17:43:11 +0000151 bool read(std::string* ErrMsg);
Misha Brukmand57308a2003-09-23 16:13:28 +0000152 };
153}
Misha Brukman12c29d12003-09-22 23:38:23 +0000154
Misha Brukman8a96c532005-04-21 21:44:41 +0000155BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
Reid Spencerdf45a542004-06-29 23:24:14 +0000156 : BytecodeReader(H)
157{
Reid Spencer0b5a5042006-08-25 17:43:11 +0000158}
159
160bool
161BytecodeStdinReader::read(std::string* ErrMsg)
162{
Reid Spencer32f55532006-06-07 23:18:34 +0000163 sys::Program::ChangeStdinToBinary();
Reid Spencer0a834722004-12-21 07:51:33 +0000164 char Buffer[4096*4];
Misha Brukman12c29d12003-09-22 23:38:23 +0000165
166 // Read in all of the data from stdin, we cannot mmap stdin...
Reid Spencer0a834722004-12-21 07:51:33 +0000167 while (std::cin.good()) {
168 std::cin.read(Buffer, 4096*4);
169 int BlockSize = std::cin.gcount();
170 if (0 >= BlockSize)
171 break;
Misha Brukman12c29d12003-09-22 23:38:23 +0000172 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
173 }
174
Reid Spencer0b5a5042006-08-25 17:43:11 +0000175 if (FileData.empty()) {
176 if (ErrMsg)
177 *ErrMsg = "Standard Input is empty!";
178 return true;
179 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000180
Misha Brukman12c29d12003-09-22 23:38:23 +0000181 FileBuf = &FileData[0];
Reid Spencer0b5a5042006-08-25 17:43:11 +0000182 if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
183 return true;
184 return false;
Misha Brukman12c29d12003-09-22 23:38:23 +0000185}
186
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000187//===----------------------------------------------------------------------===//
Andrew Lenharth558bc882005-06-18 18:34:52 +0000188// Varargs transmogrification code...
189//
190
191// CheckVarargs - This is used to automatically translate old-style varargs to
192// new style varargs for backwards compatibility.
193static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
194 Module* M = MP->getModule();
195
196 // check to see if va_start takes arguements...
197 Function* F = M->getNamedFunction("llvm.va_start");
198 if(F == 0) return MP; //No varargs use, just return.
199
200 if (F->getFunctionType()->getNumParams() == 1)
201 return MP; // Modern varargs processing, just return.
202
203 // If we get to this point, we know that we have an old-style module.
204 // Materialize the whole thing to perform the rewriting.
Chris Lattner0300f3e2006-07-06 21:35:01 +0000205 if (MP->materializeModule() == 0)
206 return 0;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000207
208 if(Function* F = M->getNamedFunction("llvm.va_start")) {
209 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000210
Andrew Lenharth558bc882005-06-18 18:34:52 +0000211 //foo = va_start()
212 // ->
213 //bar = alloca typeof(foo)
214 //va_start(bar)
215 //foo = load bar
Jeff Cohen00b168892005-07-27 06:12:32 +0000216
Andrew Lenharth558bc882005-06-18 18:34:52 +0000217 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
218 const Type* ArgTy = F->getFunctionType()->getReturnType();
219 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000220 Function* NF = M->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000221 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000222
223 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
224 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
225 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
226 new CallInst(NF, bar, "", CI);
227 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
228 CI->replaceAllUsesWith(foo);
229 CI->getParent()->getInstList().erase(CI);
230 }
231 F->setName("");
232 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Andrew Lenharth558bc882005-06-18 18:34:52 +0000234 if(Function* F = M->getNamedFunction("llvm.va_end")) {
235 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
236 //vaend foo
237 // ->
238 //bar = alloca 1 of typeof(foo)
239 //vaend bar
240 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
241 const Type* ArgTy = F->getFunctionType()->getParamType(0);
242 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243 Function* NF = M->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000244 RetTy, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000245
Andrew Lenharth558bc882005-06-18 18:34:52 +0000246 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
247 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
248 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000249 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000250 new CallInst(NF, bar, "", CI);
251 CI->getParent()->getInstList().erase(CI);
252 }
253 F->setName("");
254 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000255
Andrew Lenharth558bc882005-06-18 18:34:52 +0000256 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
257 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
258 //foo = vacopy(bar)
259 // ->
260 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000261 //b = alloca 1 of typeof(foo)
262 //store bar -> b
263 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000264 //foo = load a
Jeff Cohen00b168892005-07-27 06:12:32 +0000265
Andrew Lenharth558bc882005-06-18 18:34:52 +0000266 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
267 const Type* ArgTy = F->getFunctionType()->getReturnType();
268 const Type* ArgTyPtr = PointerType::get(ArgTy);
Jeff Cohen00b168892005-07-27 06:12:32 +0000269 Function* NF = M->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000270 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000271
Andrew Lenharth558bc882005-06-18 18:34:52 +0000272 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
273 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
274 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000275 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
276 new StoreInst(CI->getOperand(1), b, CI);
277 new CallInst(NF, a, b, "", CI);
278 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000279 CI->replaceAllUsesWith(foo);
280 CI->getParent()->getInstList().erase(CI);
281 }
282 F->setName("");
283 }
284 return MP;
285}
286
287//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000288// Wrapper functions
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000289//===----------------------------------------------------------------------===//
Misha Brukmand57308a2003-09-23 16:13:28 +0000290
291/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
292/// buffer
Misha Brukman8a96c532005-04-21 21:44:41 +0000293ModuleProvider*
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000294llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
295 unsigned Length,
Reid Spencerdf45a542004-06-29 23:24:14 +0000296 const std::string &ModuleID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000297 std::string *ErrMsg,
298 BytecodeHandler *H) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000299 BytecodeBufferReader* rdr =
300 new BytecodeBufferReader(Buffer, Length, ModuleID, H);
301 if (rdr->read(ErrMsg))
302 return 0;
303 return CheckVarargs(rdr);
Misha Brukman12c29d12003-09-22 23:38:23 +0000304}
305
Misha Brukmand57308a2003-09-23 16:13:28 +0000306/// ParseBytecodeBuffer - Parse a given bytecode buffer
307///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000308Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
309 const std::string &ModuleID,
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000310 std::string *ErrMsg) {
311 ModuleProvider *MP =
Reid Spencer0b5a5042006-08-25 17:43:11 +0000312 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000313 if (!MP) return 0;
314 Module *M = MP->releaseModule();
315 delete MP;
316 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +0000317}
318
Misha Brukmand57308a2003-09-23 16:13:28 +0000319/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
Misha Brukman12c29d12003-09-22 23:38:23 +0000320///
Reid Spencer0b5a5042006-08-25 17:43:11 +0000321ModuleProvider *
322llvm::getBytecodeModuleProvider(const std::string &Filename,
323 std::string* ErrMsg,
324 BytecodeHandler* H) {
325 // Read from a file
326 if (Filename != std::string("-")) {
327 BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
328 if (rdr->read(ErrMsg))
329 return 0;
330 return CheckVarargs(rdr);
331 }
332
333 // Read from stdin
334 BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
335 if (rdr->read(ErrMsg))
336 return 0;
337 return CheckVarargs(rdr);
Misha Brukman12c29d12003-09-22 23:38:23 +0000338}
339
Misha Brukmand57308a2003-09-23 16:13:28 +0000340/// ParseBytecodeFile - Parse the given bytecode file
341///
Chris Lattnerdeab9a72003-11-19 16:06:55 +0000342Module *llvm::ParseBytecodeFile(const std::string &Filename,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000343 std::string *ErrMsg) {
344 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000345 if (!MP) return 0;
346 Module *M = MP->releaseModule();
347 delete MP;
348 return M;
Misha Brukman12c29d12003-09-22 23:38:23 +0000349}
Brian Gaeked0fde302003-11-11 22:41:34 +0000350
Reid Spencerdf45a542004-06-29 23:24:14 +0000351// AnalyzeBytecodeFile - analyze one file
Reid Spencer4542c432004-08-21 20:52:03 +0000352Module* llvm::AnalyzeBytecodeFile(
353 const std::string &Filename, ///< File to analyze
354 BytecodeAnalysis& bca, ///< Statistical output
Reid Spencer0b5a5042006-08-25 17:43:11 +0000355 std::string *ErrMsg, ///< Error output
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000356 std::ostream* output ///< Dump output
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000357) {
Reid Spencer0b5a5042006-08-25 17:43:11 +0000358 BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
359 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000360 if (!MP) return 0;
361 Module *M = MP->releaseModule();
362 delete MP;
363 return M;
Reid Spencerdf45a542004-06-29 23:24:14 +0000364}
365
366// AnalyzeBytecodeBuffer - analyze a buffer
367Module* llvm::AnalyzeBytecodeBuffer(
Reid Spencer4542c432004-08-21 20:52:03 +0000368 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
369 unsigned Length, ///< Size of the bytecode buffer
370 const std::string& ModuleID, ///< Identifier for the module
371 BytecodeAnalysis& bca, ///< The results of the analysis
Reid Spencer0b5a5042006-08-25 17:43:11 +0000372 std::string* ErrMsg, ///< Errors, if any.
Misha Brukman5adf0ca2004-09-12 20:56:38 +0000373 std::ostream* output ///< Dump output, if any
374)
Reid Spencerdf45a542004-06-29 23:24:14 +0000375{
Reid Spencer0b5a5042006-08-25 17:43:11 +0000376 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
377 ModuleProvider* MP =
378 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000379 if (!MP) return 0;
380 Module *M = MP->releaseModule();
381 delete MP;
382 return M;
Reid Spencerdf45a542004-06-29 23:24:14 +0000383}
384
Misha Brukman8a96c532005-04-21 21:44:41 +0000385bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000386 Module::LibraryListType& deplibs,
387 std::string* ErrMsg) {
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000388 ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
Reid Spencer0b5a5042006-08-25 17:43:11 +0000389 if (!MP) {
Reid Spencere0cf59e2004-08-24 22:46:20 +0000390 deplibs.clear();
Reid Spencer0b5a5042006-08-25 17:43:11 +0000391 return true;
Reid Spencere0cf59e2004-08-24 22:46:20 +0000392 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000393 Module* M = MP->releaseModule();
394 deplibs = M->getLibraries();
395 delete M;
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000396 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000397 return false;
Reid Spencere0cf59e2004-08-24 22:46:20 +0000398}
399
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000400static void getSymbols(Module*M, std::vector<std::string>& symbols) {
Reid Spencer565ff3d2004-11-14 22:00:48 +0000401 // Loop over global variables
Chris Lattnere4d5c442005-03-15 04:54:21 +0000402 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Reid Spencer818827d2005-02-13 18:12:20 +0000403 if (!GI->isExternal() && !GI->hasInternalLinkage())
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000404 if (!GI->getName().empty())
405 symbols.push_back(GI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000406
Chris Lattnere5cea5e2005-02-13 17:42:11 +0000407 // Loop over functions.
408 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
409 if (!FI->isExternal() && !FI->hasInternalLinkage())
410 if (!FI->getName().empty())
411 symbols.push_back(FI->getName());
Reid Spencer565ff3d2004-11-14 22:00:48 +0000412}
413
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000414// Get just the externally visible defined symbols from the bytecode
415bool llvm::GetBytecodeSymbols(const sys::Path& fName,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000416 std::vector<std::string>& symbols,
417 std::string* ErrMsg) {
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000418 ModuleProvider *MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
Reid Spencer0b5a5042006-08-25 17:43:11 +0000419 if (!MP)
420 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000421
Chris Lattner0300f3e2006-07-06 21:35:01 +0000422 // Get the module from the provider
Reid Spencer0b5a5042006-08-25 17:43:11 +0000423 Module* M = MP->materializeModule();
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000424 if (M == 0) {
425 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000426 return true;
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000427 }
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000428
Chris Lattner0300f3e2006-07-06 21:35:01 +0000429 // Get the symbols
430 getSymbols(M, symbols);
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000431
Chris Lattnerf0edc6c2006-10-12 18:32:30 +0000432 // Done with the module.
433 delete MP;
Chris Lattner0300f3e2006-07-06 21:35:01 +0000434 return true;
Reid Spencer2bcfcbe2004-11-06 08:56:40 +0000435}
436
Misha Brukman8a96c532005-04-21 21:44:41 +0000437ModuleProvider*
Reid Spencer766b7932004-11-15 01:20:11 +0000438llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
Reid Spencer5a885782004-11-16 06:41:05 +0000439 const std::string& ModuleID,
Reid Spencer0b5a5042006-08-25 17:43:11 +0000440 std::vector<std::string>& symbols,
441 std::string* ErrMsg) {
442 // Get the module provider
443 ModuleProvider* MP =
444 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
445 if (!MP)
446 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000447
Reid Spencer0b5a5042006-08-25 17:43:11 +0000448 // Get the module from the provider
449 Module* M = MP->materializeModule();
450 if (M == 0) {
Reid Spencer5a885782004-11-16 06:41:05 +0000451 delete MP;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000452 return 0;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000453 }
Reid Spencer0b5a5042006-08-25 17:43:11 +0000454
455 // Get the symbols
456 getSymbols(M, symbols);
457
458 // Done with the module. Note that ModuleProvider will delete the
459 // Module when it is deleted. Also note that its the caller's responsibility
460 // to delete the ModuleProvider.
461 return MP;
Reid Spencer565ff3d2004-11-14 22:00:48 +0000462}