1 //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements loading and parsing a bytecode file and parsing a
11 // bytecode module from a given buffer.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bytecode/Analyzer.h"
16 #include "llvm/Bytecode/Reader.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/System/MappedFile.h"
26 //===----------------------------------------------------------------------===//
27 // BytecodeFileReader - Read from an mmap'able file descriptor.
31 /// BytecodeFileReader - parses a bytecode file from a file
33 class BytecodeFileReader : public BytecodeReader {
35 sys::MappedFile mapFile;
37 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
38 void operator=(const BytecodeFileReader &BFR); // Do not implement
41 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
45 BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
46 llvm::BytecodeHandler* H )
48 , mapFile( sys::Path(Filename))
51 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
52 ParseBytecode(buffer, mapFile.size(), Filename);
55 //===----------------------------------------------------------------------===//
56 // BytecodeBufferReader - Read from a memory buffer
60 /// BytecodeBufferReader - parses a bytecode file from a buffer
62 class BytecodeBufferReader : public BytecodeReader {
64 const unsigned char *Buffer;
67 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
68 void operator=(const BytecodeBufferReader &BFR); // Do not implement
71 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
72 const std::string &ModuleID,
73 llvm::BytecodeHandler* Handler = 0);
74 ~BytecodeBufferReader();
79 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
81 const std::string &ModuleID,
82 llvm::BytecodeHandler* H )
85 // If not aligned, allocate a new buffer to hold the bytecode...
86 const unsigned char *ParseBegin = 0;
87 if (reinterpret_cast<uint64_t>(Buf) & 3) {
88 Buffer = new unsigned char[Length+4];
89 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
90 ParseBegin = Buffer + Offset;
91 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
94 // If we don't need to copy it over, just use the caller's copy
95 ParseBegin = Buffer = Buf;
99 ParseBytecode(ParseBegin, Length, ModuleID);
101 if (MustDelete) delete [] Buffer;
106 BytecodeBufferReader::~BytecodeBufferReader() {
107 if (MustDelete) delete [] Buffer;
110 //===----------------------------------------------------------------------===//
111 // BytecodeStdinReader - Read bytecode from Standard Input
115 /// BytecodeStdinReader - parses a bytecode file from stdin
117 class BytecodeStdinReader : public BytecodeReader {
119 std::vector<unsigned char> FileData;
120 unsigned char *FileBuf;
122 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
123 void operator=(const BytecodeStdinReader &BFR); // Do not implement
126 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
130 BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
135 // Read in all of the data from stdin, we cannot mmap stdin...
136 while (std::cin.good()) {
137 std::cin.read(Buffer, 4096*4);
138 int BlockSize = std::cin.gcount();
141 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
144 if (FileData.empty())
145 throw std::string("Standard Input empty!");
147 FileBuf = &FileData[0];
148 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
151 //===----------------------------------------------------------------------===//
152 // Varargs transmogrification code...
155 // CheckVarargs - This is used to automatically translate old-style varargs to
156 // new style varargs for backwards compatibility.
157 static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
158 Module* M = MP->getModule();
160 // check to see if va_start takes arguements...
161 Function* F = M->getNamedFunction("llvm.va_start");
162 if(F == 0) return MP; //No varargs use, just return.
164 if (F->getFunctionType()->getNumParams() == 1)
165 return MP; // Modern varargs processing, just return.
167 // If we get to this point, we know that we have an old-style module.
168 // Materialize the whole thing to perform the rewriting.
169 MP->materializeModule();
171 if(Function* F = M->getNamedFunction("llvm.va_start")) {
172 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
176 //bar = alloca typeof(foo)
180 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
181 const Type* ArgTy = F->getFunctionType()->getReturnType();
182 const Type* ArgTyPtr = PointerType::get(ArgTy);
183 Function* NF = M->getOrInsertFunction("llvm.va_start",
184 RetTy, ArgTyPtr, (Type *)0);
186 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
187 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
188 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
189 new CallInst(NF, bar, "", CI);
190 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
191 CI->replaceAllUsesWith(foo);
192 CI->getParent()->getInstList().erase(CI);
197 if(Function* F = M->getNamedFunction("llvm.va_end")) {
198 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
201 //bar = alloca 1 of typeof(foo)
203 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
204 const Type* ArgTy = F->getFunctionType()->getParamType(0);
205 const Type* ArgTyPtr = PointerType::get(ArgTy);
206 Function* NF = M->getOrInsertFunction("llvm.va_end",
207 RetTy, ArgTyPtr, (Type *)0);
209 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
210 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
211 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
212 new StoreInst(CI->getOperand(1), bar, CI);
213 new CallInst(NF, bar, "", CI);
214 CI->getParent()->getInstList().erase(CI);
219 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
220 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
223 //a = alloca 1 of typeof(foo)
224 //b = alloca 1 of typeof(foo)
229 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
230 const Type* ArgTy = F->getFunctionType()->getReturnType();
231 const Type* ArgTyPtr = PointerType::get(ArgTy);
232 Function* NF = M->getOrInsertFunction("llvm.va_copy",
233 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
235 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
236 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
237 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
238 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
239 new StoreInst(CI->getOperand(1), b, CI);
240 new CallInst(NF, a, b, "", CI);
241 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
242 CI->replaceAllUsesWith(foo);
243 CI->getParent()->getInstList().erase(CI);
250 //===----------------------------------------------------------------------===//
252 //===----------------------------------------------------------------------===//
254 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
257 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
259 const std::string &ModuleID,
260 BytecodeHandler* H ) {
262 new BytecodeBufferReader(Buffer, Length, ModuleID, H));
265 /// ParseBytecodeBuffer - Parse a given bytecode buffer
267 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
268 const std::string &ModuleID,
269 std::string *ErrorStr){
271 std::auto_ptr<ModuleProvider>
272 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
273 return AMP->releaseModule();
274 } catch (std::string &err) {
275 if (ErrorStr) *ErrorStr = err;
280 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
282 ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
283 BytecodeHandler* H) {
284 if (Filename != std::string("-")) // Read from a file...
285 return CheckVarargs(new BytecodeFileReader(Filename,H));
286 else // Read from stdin
287 return CheckVarargs(new BytecodeStdinReader(H));
290 /// ParseBytecodeFile - Parse the given bytecode file
292 Module *llvm::ParseBytecodeFile(const std::string &Filename,
293 std::string *ErrorStr) {
295 std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
296 return AMP->releaseModule();
297 } catch (std::string &err) {
298 if (ErrorStr) *ErrorStr = err;
303 // AnalyzeBytecodeFile - analyze one file
304 Module* llvm::AnalyzeBytecodeFile(
305 const std::string &Filename, ///< File to analyze
306 BytecodeAnalysis& bca, ///< Statistical output
307 std::string *ErrorStr, ///< Error output
308 std::ostream* output ///< Dump output
312 BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
313 std::auto_ptr<ModuleProvider> AMP(
314 getBytecodeModuleProvider(Filename,analyzerHandler));
315 return AMP->releaseModule();
316 } catch (std::string &err) {
317 if (ErrorStr) *ErrorStr = err;
322 // AnalyzeBytecodeBuffer - analyze a buffer
323 Module* llvm::AnalyzeBytecodeBuffer(
324 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
325 unsigned Length, ///< Size of the bytecode buffer
326 const std::string& ModuleID, ///< Identifier for the module
327 BytecodeAnalysis& bca, ///< The results of the analysis
328 std::string* ErrorStr, ///< Errors, if any.
329 std::ostream* output ///< Dump output, if any
333 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
334 std::auto_ptr<ModuleProvider>
335 AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
336 return AMP->releaseModule();
337 } catch (std::string &err) {
338 if (ErrorStr) *ErrorStr = err;
343 bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
344 Module::LibraryListType& deplibs) {
346 std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
347 Module* M = AMP->releaseModule();
349 deplibs = M->getLibraries();
358 static void getSymbols(Module*M, std::vector<std::string>& symbols) {
359 // Loop over global variables
360 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
361 if (!GI->isExternal() && !GI->hasInternalLinkage())
362 if (!GI->getName().empty())
363 symbols.push_back(GI->getName());
365 // Loop over functions.
366 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
367 if (!FI->isExternal() && !FI->hasInternalLinkage())
368 if (!FI->getName().empty())
369 symbols.push_back(FI->getName());
372 // Get just the externally visible defined symbols from the bytecode
373 bool llvm::GetBytecodeSymbols(const sys::Path& fName,
374 std::vector<std::string>& symbols) {
376 std::auto_ptr<ModuleProvider> AMP(
377 getBytecodeModuleProvider(fName.toString()));
379 // Get the module from the provider
380 Module* M = AMP->materializeModule();
383 getSymbols(M, symbols);
385 // Done with the module
394 llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
395 const std::string& ModuleID,
396 std::vector<std::string>& symbols) {
398 ModuleProvider* MP = 0;
400 // Get the module provider
401 MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
403 // Get the module from the provider
404 Module* M = MP->materializeModule();
407 getSymbols(M, symbols);
409 // Done with the module. Note that ModuleProvider will delete the
410 // Module when it is deleted. Also note that its the caller's responsibility
411 // to delete the ModuleProvider.
415 // We delete only the ModuleProvider here because its destructor will
416 // also delete the Module (we used materializeModule not releaseModule).